# Everything Be True Challenge Help [SOLVED]

Everything Be True Challenge Help [SOLVED]
0

#1

Hi guys, can’t figure out what’s wrong with my code. Some of the trues are coming up false and I have no idea why. Please help if you can. Many many thanks in advance

/* Check if the predicate (second argument) is truthy on all elements of a collection (first argument).

Remember, you can access object properties through either dot notation or [] notation.
*/

function truthCheck(collection, pre) {
var hold = "";
// Is everyone being true?
for (var i=0; i<collection.length; i++) {
console.log(collection[i], pre, collection[i][pre]);
if(collection[i][pre] === undefined || collection[i][pre] === 0 || collection[i][pre] === "" || collection[i][pre] === null || isNaN(collection[i][pre]) === true) {
hold = false;
break;
} else
hold = true;
}

return hold;
}

truthCheck([{"single": "yes"}], "single"); // this is returning false? :(

#2

isNaN(collection[i][pre])

If you pass a string to isNaN() what you’ll get?

#3

try this!

function truthCheck(collection, pre) {

for(var i=0;i<collection.length;i++) {
if(!collection[i][pre]) {
return false;
}
}
return true;
}

#4

That worked! Thanks I knew there was a simpler way than what I did

#5
return collection.every(function(item){ return item[pre] });

#6

Very nice and clean implementation.

I was trying to do it backwards returning true first and then false but I don’t understand why it wouldn’t worked, here’s my code:

function truthCheck(collection, pre) {
for(var i = 0; i < collection.length; i++) {
if(collection[i][pre]) {
return true;
}
}
return false;
}

#7

You have to check if all values are true. In your code the function returns when it encounters the first true value.

#8

uh huh, that would actually makes a lot of sense cause when I was trying my code on python tutor and console logging the result of the if statement it will only console log once and stop.

Thanks Jenovs appreciate it

#9

Can’t wait to be like you guys when i grow up lol…it’s amazing to see how some of you come up with the simplest methods when it takes me hours to not even have it figured out, I’ve only been at this for a little over 2 months, but i’m looking forward to being able to form solutions as easy as you all.

All the best and thanks for your contributions!

#10

It’s simple. In your code when if condition is true (collection[i][pre] == true), function returns true. So if first object will satisfy the condition, for loop will be stopped and won’t check other objects in collection. That’s why we have to detect false, not true.

#11