Falsy Bouncer: Need Help!

Can someone tell me why my code removed undefined only and not false? Also, when NaN is included in arr, it isn’t removed. Any help would be appreciated!

Here is the challenge:

Remove all falsy values from an array.

Falsy values in JavaScript are false, null, 0, “”, undefined, and NaN.

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var badMatch = [false, null, 0, "", undefined, NaN];
  for (var x in badMatch) {
  var y = badMatch.indexOf(arr[x]);
  if (y > -1) {
    arr.splice(x, 1);
  }    
  }
  console.log(arr)
}

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

@JacksonBates any ideas?

What does x represent in this line:

for (var x in badMatch) {

Also, what does x represent in this line:

var y = badMatch.indexOf(arr[x]);

And, what does x represent in this line:

arr.splice(x, 1);

Think that through carefully for yourself first - then check this codepen to see if your assumptions are correct - It’s just your code with more descriptive console logging. http://codepen.io/Malgalin/pen/PbXqZP?editors=0012

You code isn’t doing what you think it is, so examine why it is doing what it is doing :slight_smile:

This exercise and the next couple are annoying. I was having a hard time trying to figure this out. The links below will help. I can say, you are able to write all the code very succinctly. Within 2-3 lines of code. When clicking on the link that takes you to the MDN page talking about Boolean objects, it fails to mention that just the word Boolean written with a capital is a function in itself that returns only things that are truthy. Also, !! will convert any value to truthy. Also, you will need to use the filter() function. Try something like this:

function bouncer(arr) {
return arr.filter(function(x) {
return SOMETHING GOES HERE;
)};
}

bouncer([7, “ate”, undefined, false, 9]);

Also, when you get to the next challenge take a look at my post. Don’t use a For loop: