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

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:

You aren’t returning anything from the function.

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

Thanks, it actually needs two return statements apparently…

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

^ that did the trick ^

5 Likes

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?

2 Likes

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…

2 Likes

Sweet. Thanks micqey!

My solution:

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

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.

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

1 Like

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

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

2 Likes
function checkPositive(arr) {
  // Add your code below this line
  return arr.every(function(val){
      return val>0;
  });
  
  // Add your code above this line
}
console.log(checkPositive([1, 2, 3, -4, 5]));
1 Like