Falsy Bouncer - is this algorithm too simple?

Tell us what’s happening:
This topic suggests converting the array values to boolean. I wasn’t sure how to do that, but I knew I could just check each value to see if it was a truthy and filter on that and so that’s what I did. Is this too simple a solution and if so did I miss the point of this exercise? (ps. how do I convert something to a boolean type?)
pps. to be perfectly clear, the code below passes the challenge, but it just seems too suspiciously easy.

Your code so far


function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(item => item);
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer

1 Like

Your solution portrays exactly what the lesson is attempting to teach. You can simply return the value via the filter which will coerce the value to a Boolean by default. Hence, only truthy values will be returned.

Looks okay since if the item is true, then it won’t be filtered.

It’s easy becasue of filter and arrow function stuffs (some devs call it lambda).

I prefer create a empty array, and in a for loop check each element, and push them if they not respond to false.

1 Like

thanks for the quick response. I wasn’t entirely sure that there was something like a type cast or something that I should be learning. (I’m too used to strongly typed languages)

in a real dev situation I’m 100% sure that I would handle it the same (with a for loop) because for loops are just so much easier to read than these “lambda” functions. Thanks for sharing your thoughts.