Algorithm: Falsy Bouncer

Tell us what’s happening:
I am not able to get the third test case to pass.

Your code so far


function bouncer(arr) {
  for(let i=0;i<arr.length-1;i++){
    switch(arr[i]){
      case false:
      case null:
      case 0:
      case "":
      arr[i] = true;
      break;
    }
  }

  console.log(arr)

  const newArr = arr.filter((index)=>{
    if(!Number.isNaN(index) && index != true && index != undefined){
      return index;
    }
  })

  console.log(newArr)

  
}

bouncer([1, null, NaN, 2, undefined])

Your browser information:

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

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

Your answer seems a bit complicated. The first part where you replace the false values isn’t necessary.

You could simply use the filter method combined with a conditional to filter out the falsy values and return only truthy ones.

you need far simpler things to solve this algorithm, you just need to use the property of falsy values, they evaluate as false when the code expect a boolean and coerce the value to boolean!

maybe check this:




https://j11y.io/javascript/truthy-falsey/

I did the switch case as it was mentioned in the problem to convert every falsy to a boolen.
Anyway I have solved the problem and i thank you for all the help provided.

it was intended more like this: