Falsy Bouncer alternative solution

Hey there, I’ve been checking in the the forum but I haven’t seen in here the way I’ve approached it. Hope you can find it helpful.

function bouncer(arr) {
// create new array to push the desire values in
  const newArr = []
// map through the array and we "convert" to boolean every item in the array
    //  if the item is true (string, number, etc) we push it into the new array using a ternary operator
  arr.map( item => !!item === true && newArr.push(item) )
// we return the new array that holds the values that aren't undefined, false, 0 or NaN
  return newArr;
}

It’s an anti-pattern to use map as a forEach. You should only use map if you are assigning the result to a variable. The idea is that map is for making a new array by modifying the contents of the old array.

Flatmap or filter are better in this case. Filter is for removing elements from an array.


A couple of other notes

  • I don’t see a ternary, but your comment mentions one?
  • Comparing against true is redundant

HI @Inked!

Welcome to the forum!

I have added spoiler tags around your solution just for those who have not worked on this challenge yet.

Thanks!

1 Like

Thank you for your time reviewing it. I will check and try it with the advice!

1 Like