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

Use the every Method to Check that Every Element in an Array Meets a Criteria
0
#1

I think I may have found a glitch in the Functional Programing section in the “Use the every Method to Check that Every Element in an Array Meets a Criteria” lesson…

I think I’m using the right code:

``````function checkPositive(arr) {
arr.every( (num) => { return num > 0; } );
}
``````

I’ve also attempted it the way the sample shows, like this:

``````function checkPositive(arr) {
arr.every(function(currentValue) {
return currentValue > 0;
});
}
``````

When I run the tests I get correct answers on the two arrays that should return false, but then I get this:

``````// running test
checkPositive([1, 2, 3, 4, 5]) should return true.
// tests completed
``````

Can anyone help me see what I’m doing wrong? Or is this challenge just a little bungled?

Edit:
after looking at the moz developer pages for every() and some(), it seems they may be recieving empty arrays:

Use the every Method to Check that Every Element in an Array Meets a Criteria - bringing up an old question
#2

You aren’t returning anything from the function.

4 Likes
#3
``````function checkPositive(arr) {
return arr.every(n => n > 0)
}
``````
2 Likes
#4

Thanks, it actually needs two return statements apparently…

return arr.every( (num) => { return num > 0; } );

^ that did the trick ^

5 Likes
#5

I had the same question. Do you know why two return values are needed here? And is it a typo that the example expression only had one return? If it’s not a typo, why does the challenge solution need the extra return but the example solution doesn’t?

1 Like
#6

In my example, the checkPositive function needs to return the result of the function “every()” which also needs a return statement for each iteration to do it’s job properly…

so it’s something like this:

every() is checking each element of the array and returning true / false to the surrounding checkPositive function…
but if that surrounding checkPositive function isn’t returning anything, then it’s like the checkPositive function kind knows what every() returned, but it’s not telling anyone…

1 Like
#8

Sweet. Thanks micqey!

#9

My solution:

``````return arr.every(x => x >= 0);
``````
1 Like
#10

any function needs to return something ,and if it has no value to return is gonna return undefined so ,we have in this challenge two functions , checkPositive should return something and also ,we have every function wich should also return its value.

#11

Or put the function inside of a variable and return the variable.

1 Like
#12

my solution

``````function checkPositive(arr)
{
let result = arr.every(function(isPositive)  {
return isPositive >= 0;
});
return result;
}
let output = checkPositive([1, 2, -3, 4, 5]);
console.log(output);

``````
#13

you should have to use return key word before the arr object.
the following works for me.

return arr.every(function(currentValue){
return currentValue > 0;
});

1 Like
#15
``````function checkPositive(arr) {