WHY i am getting false for this array

Tell us what’s happening:

Your code so far


function checkPositive(arr) {
// Only change code below this line

return arr.every >= 0;

// Only change code above this line
}
console.log (checkPositive([1, 2, 3, 4, 5]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Use the every Method to Check that Every Element in an Array Meets a Criteria

Link to the challenge:

arr.every is a function. When you’re trying to compare two things one of which is not a number, JS engine will try to convert (coerce) that thing into number. Something like this:

Number(arr.every); // NaN

After coercion it will perform the comparison:

NaN >= 0; // false

You can read about .every() method here:

2 Likes

The way to use .every( ) seems to be this:

  1. you need to call it on an array
  2. pass the function that you want to use to check the values as a parameter to .every( ),

In this case, your checkPositive(arr) function should just return arr >=0 and then your call to the function should be the
array.every(checkPositive)

Here’s the version that worked for me:

function checkPositive(arr) {  
//here `arr` actually will be the value of the individual   
//elements in the array

return arr >= 0;  

// Only change code above this line
}
console.log ([1, 2, 3, 4, 5].every(checkPositive));//  here you are calling the `every` method on the array, and passing each element through the checkPositive function.

//=>this time it returns `true`!

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Thanks for taking care of that. I’m sorry, I really should have known better.