Everything Be True - help

Tell us what’s happening:
I know that I can just use the || operator to check if the value is equal to any of the falsy values but I wanna know why I just can’t use the code I have below. Can someone explain please?

Your code so far


function truthCheck(collection, pre) {
  collection.map(x => {
    if (!x[pre]) {
      return false;
    }
  });
  return true;
}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true

I think this is becasue of this line

return false here supposed to back to top layer collection.map callee, instead of return to truthCheck callee. in other word, return false in map arrow function is a signal to go for next element, not sure even you should return anything here, even return could work.

please note the arrow function, yes it’s a function, so return here means return to this function caller which is map here, and it ignores even true, false or … Better say, map is not suitable for this challenge.

So it means return true always invoked

I’m not sure if break work with map to ignore checking rest values, but admit js devs like these new arrow/streaming stuffs so much.

I prefer go with basic/plain for loop to allows you return to top function invoker at any place, or break and ignore rest for loop.

The fix is easy, have a variable before you go for mapping on collection with default value true.

if you like and tend to work with map, then
in collection map, once the check is false, set the res variable false, return false also here doesn’t work. the bad part is(again I don’t know, maybe possible) map continue with rest elements even it should’t.

finally instead of return true, return the res.

but if you like try the for loop(my suggestion) so it goes much better (tip: try it yourself first)

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

keep goin on great work, happy programming

1 Like

Oh my god… You know this EXACT same thing happened to me earlier today in another challenge haha. I figured that other problem out myself but for some reason couldn’t see it on this challenge. I keep forgetting that the return value is given to map() and not the parent function. Thanks for the help, man. I’m gonna try it out myself then check your suggestion if I really can’t get it.

I made it work with map() but you’re right that I should just use a for loop so the function stops once it finds a falsy value. Thanks again!

Happy to hear it

You are one passionated hard working dev, I admire people like you. Soon you will be a goody good dev, just be patient.

keep going on great work, happy programming

1 Like