marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.03k stars 799 forks source link

Your Own Loop exercise 5.3 #365

Closed kapreski closed 6 years ago

kapreski commented 6 years ago

Big thank you for the book, really.

if the condition in the test function changed to something like == the result is always true if the array is empty. in the some() version the some function runs but we are flipping the result so it's always true if array is empty. in the for of version the array is empty so it always short-circuit? I added a condition to check if the array is empty and return false if it is.. function every(array, test){ return array.length > 0 && !array.some(el => !test(el)); } what do you think? did I miss something?

marijnh commented 6 years ago

every should return true for empty arrays (since there are no elements, every element does satisfy the predicate).

I'm not quite sure what you are asking about the loop exercise. Could you clarify?

kapreski commented 6 years ago

I though the result should be false when we check against an empty array.. it's like saying: is every element in [] == 5 the array is empty [] yet the answer is true. maybe I'll understand the logic later but since Array.prototype.every()works the same way I guess the question is beyond the book..