Basic Algorithm Scripting - Falsy Bouncer

I thought this code was pretty clean. But obviously I wrong. I’m not sure
where I went wrong here. filter() is supposed to filter() out what you don’t want in a array right?

Your code so far


function bouncer(arr) {
  return arr.filter(function(element) {
    return element !== "false";
  });
}

bouncer([7, "ate", "", false, 9]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.1 Safari/605.1.15

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

return element !== false; you can change this line to return Boolean(element); I think this should be work.

DAMN! It worked! Thanks a lot!
Hey could you tell me what my mistake was?

This will return only elements which explicitly do not equal the string ‘false’. Note also that return element !== false would also only return values which do not explicitly equal false.

If you are trying to determine if a value is falsy or truthy, there are other approaches.

One very useful concept is this:

let a = true;

// if a is a truthy value
if (a) { console.log("a is a truthy value") }

// if a is a falsy value
if (!a)  { console.log("a is a falsy value") }

This is a shorthand for determining whether a value is truthy or falsy. You could change the assignment value of a to any value to test it. For instance a = "false" is actually true, because it’s just a string and strings are truthy.

2 Likes

Hey thanks for the help anyway man. Your English doesn’t have to be perfect, I’m studying 3 other languages myself.

Thanks for breaking this down into a concept I can easily get. It is best to break complex problems into simple concepts. Thanks a lot for the help man, much appreciated.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.