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

Tell us what’s happening:
My code doesn’t seem to solve the problem, I checked the solution provided and it doesn’t work either, I tried doing a return on the first function as well and that didn’t seem to work either, any advice/tips would be greatly appreciated, thanks!

Your code so far


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

  // Add your code above this line
}
checkPositive([1, 2, 3, -4, 5]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria

Can you explain how you expect currentValue > true to work?

It won’t it seems irrelevant to the rest of the code, so I took it out

What I mean is what do you expect it to do? When you wrote it, how did you expect it to function? I’m trying to figure out your thought process so I can help you effectively.

Bro arr.every return Boolean result (true/false) so declare variable to receive the test result from the function then return this variable as result

1 Like

currentValue > true Can you explain this comparison?

ETA: This reply was unintentionally made to @SalimDev rather than @DevinCassidy. Sorry about that.

you’re right the comparaison should be currentValue > 0 to test if positive

Spoiler

this function should be like this

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

But why would that work?

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

Thats why I asked why this would work, because I almost solved it but with his solution I have it solved but not exactly sure why

The every() method applies the condition on every element on the array and if all the elements return true then the method will return true otherwise it will return false even if only one returned false.

You’re right :ok_hand:

  1. Your function is not explicitly returning anything, so it will return undefined.

  2. currentValue is each of the elements inside the array, they are in this case, numbers.

currentValue > true

  1. The Boolean value true cannot (without coercion) be greater or smaller than a number. So the Boolean value true will get coerced to the numeric value of 1 (true coerces to 1 and false to 0)
1 == true
true

0 == false
true

true > 0
true

false < 1
true

Remember true is now coerced to the number 1

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

  // Add your code above this line
}

checkPositive([2, 3, 5])
true

Remember false is now coerced to the number 0

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

  // Add your code above this line
}

checkPositive([1, 2, 3, 5])
true