So I made it through this challenge, with the help of the hints (OK…and also the spoilers!). What I’m not sure about, though, is why my original solution was returning “false” for all possible inputs. Here is the code:
function truthCheck(collection, pre) {
for (let i = 0; i < collection.length; i++) {
if (collection[i].hasOwnProperty(pre) === false) {
return false;
}
else if (Boolean(collection[i].pre) === false) {
return false;
}
}
return true;
}
What I thought this for-loop would do:
1: Iterate through every object in the array
2: Test if the object had the pre
property; label the collection “false” if not
3: If the object did have pre
, then test to see if its value was truthy; label the whole collection “false” if not
4: Finally, if the entire loop is completed without 2 or 3 finding anything, return true.
Why didn’t I ever get to scenario 4 in this code? I understand why the solutions to this challenge work, but I also want to know why this solution didn’t work. Thanks!
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true/