The goal of the ‘Everything Be True’ intermediate algorithm is to check if the predicate (second argument) is truthy on all elements of a collection (first argument). Each of the listed 3 function calls should return TRUE. I solved the algorithm in this manner:
function truthCheck2(collection, pre) {
return collection.every(function(element) {
return element[pre];
});
}
console.log("(element[pre])");
console.log("True: "+ truthCheck2([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"));
console.log("True: "+ truthCheck2([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat"));
console.log("True: "+ truthCheck2([{"single": "yes"}], "single"));
All three tests return TRUE as expected. However, when I tried to determine if the predicate was truthy using either === or == the first and the third function calls failed. Any idea why using == or === fail to find the truthy?
function truthCheck1(collection, pre) {
return collection.every(function(element) {
return element[pre] === true || element[pre] == true;
});
}
console.log("(element[pre] === true || element[pre] == true)");
console.log("True: "+ truthCheck1([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"));
console.log("True: "+ truthCheck1([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat"));
console.log("True: "+ truthCheck1([{"single": "yes"}], "single"));
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36