Implement a Falsy Remover - Implement a Falsy Remover

Tell us what’s happening:

I’ve got most of the tests passing, but I’m still stuck on test 2 and test 3.

Test 2: bouncer([7, “ate”, “”, false, 9]) should return [7, “ate”, 9], but my code only gives [7, 9].

Test 3: bouncer([“a”, “b”, “c”]) should return [“a”, “b”, “c”], but mine comes back as .

Not quite sure where I’m going wrong here — any tips?

Your code so far

let newArray = [];

const bouncer = (array) => {
  array.forEach((arrEl) => {
    if (
      arrEl !== false &&
      arrEl !== null &&
      arrEl !== 0 &&
      arrEl !== "" &&
      arrEl !== undefined &&
      !isNaN(arrEl)
    ) {
      newArray.push(arrEl);
    }
  });
  return newArray;
};

Your browser information:

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

Challenge Information:

Implement a Falsy Remover - Implement a Falsy Remover
https://www.freecodecamp.org/learn/full-stack-developer/lab-falsy-remover/implement-a-falsy-remover

I worked on this one yesterday. I used the hint provided by freeCodeCamp to convert each value into a Boolean.

I’ve finally passed all the tests with your help. I hadn’t realised just how useful Boolean can be — that was a real eye-opener!

1 Like

you can also use the inherent truthyness or falsyness of any value here

Do you mean I can rely on JavaScript’s type coercion — where values are automatically treated as either truthy or falsy — instead of explicitly wrapping them in Boolean()?

Because both if (value) and if (Boolean(value)) work the same way.

yes, it’s rare you need to use Boolean specifically when you can do that

1 Like