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) {
  // Add your code below this line
  arr.every( (num) => { return num > 0; } );
  // Add your code above this line
}

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

function checkPositive(arr) {
  // Add your code below this line
  arr.every(function(currentValue) {
    return currentValue > 0;
  });
  // Add your code above this line
}

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.


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

#4

Thanks, it actually needs two return statements apparently…

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

^ that did the trick ^


#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?


#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…


#8

Sweet. Thanks micqey!


#9

My solution:

return arr.every(x => x >= 0);

#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.


#12

my solution :slight_smile:

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