10 Days Of JavaScript

Day 4: Count Objects Solution

Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 4: Count Objects Solution.

Also visit this link:ย  Day 4: Create A Rectangle Object Solution


Day 4: Count Objects Solution


In this challenge, we learn about iterating over objects.


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ย ==ย o.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.


  • 5 <=ย nย <= 10
  • 1 <=ย x,ย yย <= 100

Output Format

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

Sample Input 0

1 1
2 3
3 3
3 4
4 5

Sample Output 0


Explanation 0

There areย nย = 5ย objects in theย objectsย array:

  • objects0ย = {x: 1, y: 1}
  • objects1ย = {x: 2, y: 3}
  • objects2ย = {x: 3, y: 3}
  • objects3ย = {x: 3, y: 4}
  • objects4ย = {x: 4, y: 5}

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


Solution โ€“ Day 4: Count Objects

'useย strict';


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++];

ย *ย Returnย aย countย ofย theย totalย numberย ofย objectsย 'o'ย satisfyingย o.xย ==ย o.y.
ย *ย 
ย *ย Parameter(s):
ย *ย objects:ย anย arrayย ofย objectsย withย integerย propertiesย 'x'ย andย 'y'
ย */
functionย getCount(objects)ย {
ย ย ย ย varย countย =ย 0;
ย ย ย ย forย (varย indexย inย objects){
ย ย ย ย ย ย ย ย ifย (objects[index].xย ==ย objects[index].y){
ย ย ย ย ย ย ย ย ย ย ย ย count++;
ย ย ย ย ย ย ย ย }
ย ย ย ย }
ย ย ย ย returnย count;

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));

Complete the function in the editor. It has two parameters: a and b. It must return an object modeling a rectangle that has the following properties: Complete the function in the editor. It has two parameters:ย aย andย b. It must return an object modeling a rectangle that has the following properties: