Why doesn't my solution work? - Basic Algorithm Scripting - Falsy Bouncer

This is my first pass at this challenge and I’ve got some other ideas for solving it, but I’m wondering why my code doesn’t fulfill the requirements. It looks as though my push is ignoring my if statement. Seems to me it should only be pushing the correct values into my new array but it’s pushing everything in. I’m obviously using the wrong approach, but could someone help me understand why this isn’t working? Thanks!

function bouncer(arr) {
  const newArray = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== false || arr[i] !== null || arr[i] !== 0 || arr[i] !== "" || arr[i] !== undefined || arr[i] !== NaN) {
      newArray.push(arr[i]);
    }
  }
  return newArray;  
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

Let’s see why using the above test case.

In the first iteration, arri[i] is really arr[0] which is 7 and your if statement’s first condition checks if 7 is not strictly equal to false (which is not not), so the 7 is pushed to newArr.

In the second iteration, arri[i] is really arr[1] which is ate and your if statement’s first condition checks if ate is not strictly equal to false (which is not not), so the ate is pushed to newArr.

In the third iteration, arri[i] is really arr[2] which is an empty string and your if statement’s first condition checks if an empty string is not strictly equal to false (which is not not), so the empty string is pushed to newArr.

In the fourth iteration, arri[i] is really arr[3] which ‘false’ and your if statement’s first condition checks if ‘false’ is not strictly equal to false. This evaluates to false, so the next condition after the || checks if false is not strictly equal to null (which is not not), so false is pushed to newArr.

In the first iteration, arri[i] is really arr[4] which is 9 and your if statement’s first condition checks if 9 is not strictly equal to false (which is not not), so the 9 is pushed to newArr.

If you go back and review the Comparisons with the Logical Or Operator, you will discover that if an if statement is made of conditions only separated by the || operator, all it takes is one of the conditions to evaluate to true for the entire statement to be true.

You are going to need to rethink your logic here.

That makes sense. Thanks! It feels like it was years ago since I went through that challenge, though it’s probably only been a week or two.

I was able to figure out a way but had to do a google search to figure out what to do with NaN. That was real tricky.


function bouncer(arr) {
  const newArray = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== false && arr[i] !== null && arr[i] !== 0 && arr[i] !== "" && arr[i] !== undefined && Boolean(arr[i]) === true) {
      newArray.push(arr[i]);
    }
  }
  return newArray;  
}

What you were supposed to learn from this challenge is you do not need any of:

arr[i] !== false && arr[i] !== null && arr[i] !== 0 && arr[i] !== "" && arr[i] !== undefined && 

I know, I have to do major refinements to the way I write as I usually have tons more lines of code than the solutions provided. I always check the solutions after I finish mine and see how inefficient my code is. That said, 4 days ago I literally was just staring at the computer screen wondering how I was supposed to know how to solve any of these, and then something in my brain clicked (thank God). I’ll probably come back after I finish the entire Javascript certification and redo these with a focus on minimizing my code.