-1

I built a function every that should iterate over an array and return true if the action (for ex. element < 10) performed on all the elements is true. Here's my code:

function every(array, action) {
  var trueOrFalse = true
  for (var i = 0; i < array.length; i++)  
    trueOrFalse = trueOrFalse && action(array[i]);
  if (trueOrFalse = true) return true;
  else  return;
}
array1 = [1,2,3,4,5,6,7,8,9,10,11]
console.log(every(array1, function(element) {
  return element < 10 
}))

I don't see anything wrong. With array1 it returns true even if it contains numbers > 10. Where's the problem?

Thanks

7
  • Optimization idea: you don't need to iterate the rest of the elements once you found the first one that does not satisfy your condition. It's called "short circuiting". As a bonus, this will also eliminate your boolean calculation code and the bug which hides there. Commented May 10, 2017 at 15:20
  • 2
    There is a built-in array method for this: Array.prototype.every. Commented May 10, 2017 at 15:22
  • Is anyone going to mention, why not use the built in array.prototype.every() method? Commented May 10, 2017 at 15:23
  • Don't use the variable at all. The first time that action gives a falsey result, return false immediately. Then put return true after the loop. for (var i = 0; i < array.length; i++) { if (!action(array[i])) return false } return true Commented May 10, 2017 at 15:25
  • Yes, I considered that option. Was just curious to know why mine dind't work. Commented May 10, 2017 at 15:34

4 Answers 4

4
if (trueOrFalse = true) return true; 

should be

if (trueOrFalse == true) return true;
Sign up to request clarification or add additional context in comments.

4 Comments

This. trueOrFalse = true is an assignment. It sets trueOrFalse to true - and then incidentally returns the value it just set, so the condition is always true.
if (trueOrFalse == true) should be if (trueOrFalse)
Why not just return trueOfFalse?
@Mark Reed Thanks! For a beginner like me that was the most helpful comment.
1

You need to evaluate trueOrFalse is equal to true For which you need the double equals

if (trueOrFalse == true) return true;

Other wise you'll just be making the value of trueOrFalse the same as true

Bonus points:

 if (trueOrFalse === true) return true;

Using three equal signs is evaluating exactly the same type and value. That isn't required here but is useful to know.

4 Comments

could use more explanation than just a line of corrected code.
I assumed it was a typo and didn't require it
@MrMysteryGuest If you believed it was a typographical error, you should flag the answer as such and move on.
@mhodges I'll do that from now on.
0

Your condition is using an incorrect operator, you should be using the == operator.

You can use if (trueOrFalse == true) return true;

OR

you can write it as if (trueOrFalse) return true; which will still evaluate it as if( true )

Comments

0

You can remove the if statement and rely on good old boolean algebra!

function every(array, action) {
  var trueOrFalse = true
  for (var i = 0; i < array.length; i++)  
    trueOrFalse = trueOrFalse && action(array[i]);
  return trueOrFalse;
}
array1 = [1,2,3,4,5,6,7,8,9,10,11]
console.log(every(array1, el => el < 10));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.