0

I am using a function that takes an array and returns true if all its values are even and false otherwise.

My Code is below and passed array is [ -2, 2, -2, 2 ]

function checkAllEven(arr) {
  return arr.every(x=> { x % 2 === 0 });
}
-------------
Output: False

But when I removed curly braces, it works correctly and return true.

function checkAllEven(arr) {
  return arr.every(x=> x % 2 === 0 );
}
-------------
Output: True

Just Want to know why it is happening.

Ravi Sharma
  • 409
  • 6
  • 19

1 Answers1

1

You should use return if you have curly braces:

function checkAllEven(arr) {
  return arr.every(x=> { return x % 2 === 0 });
}
StepUp
  • 30,747
  • 12
  • 76
  • 133