Day 4: Count Objects | 10 Days of JavaScript – Hacker Rank Solution

Day 4: Count Objects | 10 Days of JavaScript – Hacker Rank Solution: Hey Guy’s In this post we are solving Day 4: Count Object which is a part of 10 Days of JavaScript Series.

Day 4: Count Objects | 10 Days of JavaScript – Hacker Rank Solution

Objective

In this challenge, we learn about iterating over objects. Check the attached tutorial for more details.

Task

Complete the function in the editor. It has one parameter: an array, a, of objects. Each object in the array has two integer properties denoted by x and y. The function must return a count of all such objects o in array a that satisfy o x x == o x y.

Input Format

The first line contains an integer denoting n.
Each of the n subsequent lines contains two space-separated integers describing the values of x and y.

Constraints

  • 5 <= n <= 10
  • 1 <= xy <= 100

Output Format

Return a count of the total number of objects o such that o x x == o x y. Locked stub code in the editor prints the returned value to STDOUT.

Sample Input 0

5

1 1

2 3

3 3

3 4

4 5

Sample Output 0

2

Explanation 0

There are n = 5 objects in the objects array:

Because we have two objects o that satisfy o x x == o x y (i.e., objects0 and objects2), we return 2 as our answer.

Day 4: Count Objects | 10 Days of JavaScript – Hacker Rank Solution

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
});
function readLine() {
    return inputString[currentLine++];
}
function getCount(objects) {
    return objects.filter(function(o){return o.x==o.y}).length
}
function main() {
    const n = +(readLine());
    let objects = [];
    for (let i = 0; i < n; i++) {
        const [a, b] = readLine().split(' ');
        objects.push({x: +(a), y: +(b)});
    }
    console.log(getCount(objects));
}

Disclaimer: The above Problem (Day 4: Count Objects) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment