I am going through this challenge on FCC and I am literally half way there!
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
function truthCheck(collection, pre) {
// Is everyone being true?
for(var i = 0; i < collection.length; i++){
var arr = collection[i];
for(pre in arr){
if (isNaN(arr[pre]) ){
pre = false;
return pre;
} else if (arr[pre]){
pre = true;
return pre;
}
}
}
}
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
In the introduction I said I was half way there. That's because when I evaluate the truthy values first:
if (arr[pre]){
pre = true;
return pre;
}
all the 'truthy' tests pass.
So I suppose I should be evaluated for 'truthtiness' in a different way? I say this because my code as is gets all the 'falsey' values to pass...
Thanks all!
It's false if falsey for any one of them, so test that. Then if none of them are falsey, return true.
function truthCheck(collection, pre) {
for(var i = 0; i < collection.length; i++){
if (!collection[i][pre]) { return false; }
}
return true;
}