Quantcast
Channel: How do I check if an array includes a value in JavaScript? - Stack Overflow
Viewing all articles
Browse latest Browse all 66

Answer by Mitul Panchal for How do I check if an array includes a value in JavaScript?

$
0
0

It has one parameter: an array numbers 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 in the array that satisfy numbers.x == numbers.y

var numbers = [ { x: 1, y: 1 },
                 { x: 2, y: 3 },
                 { x: 3, y: 3 },
                 { x: 3, y: 4 },
                 { x: 4, y: 5 } ];
    count = 0; 
var n = numbers.length;
for (var i =0;i<n;i++)
{
  if(numbers[i].x==numbers[i].y)
  {count+=1;}
}

alert(count);

Viewing all articles
Browse latest Browse all 66

Latest Images

Trending Articles





Latest Images