Falsy Bouncer - JavaScript

Two problems with testing for every possible falsy value:

  1. Your list is incomplete

  2. The comparison NaN === NaN will always fail

what’s the value of NaN === NaN?
that’s one thing.

Try it, in your console, somewhere…

1 Like

@JeremyLT please stop, I have already addressed why #1 is irrelevant. For #2, you are again telling me something will fail without explaining why it will fail and then giving me code to “prove” you are right. I’m respectfully requesting that you not comment on this thread anymore. Thank you for taking the time but it’s not helpful.

@ilenia thank you for taking the time but I have no idea what you’re talking about.

I am a beginner so pardon my ignorance.

I think I fixed the changing array length issue by using a new array and the push() action but I’m obviously still misunderstanding something about falsy. Here’s my code:

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

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

Blockquote

Thank you for trying to help. If you comment, please explain the issues with my specific code and not what a more elegant or efficient code would look like. Thanks. h

Aside from reiterating the issues already mentioned above (in regards to using comparisons on NaN, the code you have now will not remove anything from the array as at least one of the conditions on your if statement will be true for every value.

1 Like

can you try writing somewhere console.log(NaN === NaN) and see what appear?
what do you think NaN === NaN evaluate to? true or false? what happens when you try it?


in your new code I still see the same error… plus a new one. 0 != false is true, as you have a series of expressions with the OR operator, even one of the expressions evaluating to true, all the logical expression evaluate to true, meaning you are keeping everything.

1 Like

@nhcarrigan thank you for responding but when you reread the above posts, how many times did you read that I didn’t understand the (lack of) explanation? Therefore, you “reiterating” that explanation isn’t adding anything or being helpful at all. Not trying to be ungrateful but this is frustrating and everyone keeps commenting by effectively copying and pasting what someone else said even though I’ve stated multiple times that I don’t understand that explanation and would like it explained in a different way. I’m starting to think you all are just trolling me.

I do understand the second part of what you are saying. I can’t use != and || at the same time. Is that it?

it returned false. if that is supposed to be illuminating, i don’t understand. i do understand the second part of what you are saying. i will update using ===

The || is the OR operator. So if (a !== 1 || a !== 2) would run for any value, because at any given time a will not equal 1 or a will not equal 2.

Using the || means the if condition runs if any one of the statements is true. :slight_smile:

i said i understand that

@ilenia is trying to demonstrate that you cannot use NaN in a comparison. As you saw, NaN === NaN returns false, which means checking for arr[i] === NaN would return false, even if arr[i] was NaN.

It is perfectly normal for a beginner to want to check for all the values listed in the challenge, I’d say it’s almost expected. But I think that is the “hidden” lesson behind this challenge.

You are definitely expected to research the concept of falsy and truthy and in the process learn about how best to check for such values. That may seem like a bit of a cheap shot because the challenge doesn’t really hint at much other than listing the falsy values.

But checking for such values (falsy/truthy) is so common and something you will have to do very often in your code that it is an important concept for you to research.

I’d suggest you let go of your initial idea and try something new. In the ES6 section, you were also introduced to a specific array method that is pretty handy here.

Some links

2 Likes

can you please explain it to me like i’m 8 years old? i don’t even understand what NaN === NaN means. this is what i’m trying to tell you. - i’m a beginner.

NaN === NaN translates to “Is NaN strictly equal to NaN?”. In this case, it returns false, meaning “NaN is NOT strictly equal to NaN”.

Does that help clear it up? :slight_smile:

you used

if arr[i] is NaN and NaN === NaN actually return false, would this be able to individuate a NaN value? No, it wouldn’t, because checking NaN === NaN return false.

so what is === supposed to be used for if not to check whether things are equal to each other? my understanding was that’s what it’s supposed to do.

so what is === supposed to be used for?

that is the comparison operator
but it doesn’t work with NaN