Falsy Bouncer need help on this

Falsy Bouncer need help on this
0

#1

I’ve been away for awhile reading up on scope and object prototypes and came back to try and finish this challenge. It seems to work perfectly for everything except null. I understand null is in a category by itself in JS and it has unique attributes. I’m sure one of these attributes is the reason why my code will not work. Can anyone give me a hint as to what I am doing incorrectly? Thank you in advance. Here is my code:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArray = [];
  var badListArray = [null, false, 0, "", undefined, NaN];
  for (i = 0; i < arr.length; i++) {
    if (badListArray.indexOf(arr[i]) === -1 ) {
      newArray.push(arr[i]);}
     
    }
   
    
  
  return newArray;
}
bouncer([1, null, NaN, 2, undefined]);

Your code so far

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArray = [];
  var badListArray = [null, false, 0, "", undefined, NaN];
  for (i = 0; i < arr.length; i++) {
    if (badListArray.indexOf(arr[i]) === -1 ) {
      newArray.push(arr[i]);}
     
    }
   
    
  
  return newArray;
}
bouncer([1, null, NaN, 2, undefined]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:54.0) Gecko/20100101 Firefox/54.0.1 Waterfox/54.0.1.

Link to the challenge:


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

What you are trying to do will not work, because inn JavaScript, NaN has the distinction of being the only value that is not equal to itself. That means we can’t find out whether a value is NaN by checking equality to NaN because the answer will always be no.


#4

NaN seems to check out fine. The code will only push values that are not found in arr into newArray. That is where i am a little confused. It seems that when the for loop iterates across null, the conditional that should stop it from being pushed is instead clearing it as something that is not found in the arr. I’ve added some additional code into the if conditional which is intended to single out null intentionally. Here is the modified line:

if (badListArray.indexOf(arr[i]) === -1 && badListArray.indexOf(arr[i]) !== null)

It is giving the same result. If this situation was being caused by NaN, it would make more sense. But null is supposed to be equal to itself and to undefined. What am I missing?


#5

Oh wait the null that is getting passed into newArray is not the same null getting iterated over! Just realized something. Its coming from NaN isnt it?! OK one step closer to solving this! Lightbulb moment!


#6

Try running this code (your solution with added console.log statements):

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArray = [];
  var badListArray = [null, false, 0, "", undefined, NaN];
  for (i = 0; i < arr.length; i++) {
    console.log('arr['+i+'] = ' + arr[i] + ' | badListArray.indexOf(arr['+i+']) is ' + badListArray.indexOf(arr[i]));
    console.log('if condition evaluates to ' + (badListArray.indexOf(arr[i]) === -1 && badListArray.indexOf(arr[i]) !== null) + ' so push ' + arr[i] + ' into newArray');
    console.log(''); // blank line
    if (badListArray.indexOf(arr[i]) === -1 && badListArray.indexOf(arr[i]) !== null) {
      newArray.push(arr[i]);
    }
     
  }
  return newArray;
}
bouncer([1, null, NaN, 2, undefined]);

It will display the following:

arr[0] = 1 | badListArray.indexOf(arr[0]) is -1
if condition evaluates to true so push 1 into newArray

arr[1] = null | badListArray.indexOf(arr[1]) is 0
if condition evaluates to false so push null into newArray

arr[2] = NaN | badListArray.indexOf(arr[2]) is -1
if condition evaluates to true so push NaN into newArray

arr[3] = 2 | badListArray.indexOf(arr[3]) is -1
if condition evaluates to true so push 2 into newArray

arr[4] = undefined | badListArray.indexOf(arr[4]) is 4
if condition evaluates to false so push undefined into newArray

The reason NaN still shows up in your final newArray, is because you if statement evaluates to true. Why does it evaluate to true? Because badListArray.indexOf(arr[2]) which is badListArray.indexOf(NaN) yields -1. It is yielding -1 because it is trying to find the index of an element in badListArray which equals NaN. It can not find it, because NaN is not equal to NaN (see example below)

var funny = NaN;
var sad = NaN;
console.log (funny === sad) // this yields false

NaN is one of those quirky JS types that is not equal to itself.


#7

Thank you for all of your help. I figured out a solution (albeit not an efficient one). What I did was try to test for NaN the only way I know how, by making sure that the index value that was being iterated WAS equal to itself. If that failed, the value would not be pushed into newArray. I know there are much better ways to complete this task, but I’m just happy that it works. Here is the result I got that is functional:

  // Don't show a false ID to this bouncer.
  var newArray = [];
  var badListArray = [null, false, 0, "", undefined, NaN];
  for (i = 0; i < arr.length; i++) {
    if ((badListArray.indexOf(arr[i]) === -1) && (arr[i] === arr[i])) {
      newArray.push(arr[i]);}
     
    }
   
    
  
  return newArray;
}
bouncer([1,NaN,99,null,  "", false, 0]);```

#8

it is ok that your solution is not the most efficient. The important thing is you figured out how to deal with the NaN value with the arr[i] === arr[i] part of the if condition.

Because you now have a working solution, I will share a solution that drives home the point of the challenge.

function bouncer(arr) {
  var newArray = [];
  for (i = 0; i < arr.length; i++) {
    if (arr[i]) {
      newArray.push(arr[i]);}
    }
  return newArray;
}
bouncer([1,NaN,99,null,  "", false, 0]); // yields [1, 99]

In the above solution, I check if arr[i] evaluates true. It just some happens that in JavaScript, every value evaluates to true (in a boolean context) except false, 0, '', null, undefined, and NaN which evaluate to false. See the link below for more information.