Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bonfire: Falsy Bouncer - contradictory instructions #5303

Closed
kokushozero opened this issue Dec 15, 2015 · 3 comments
Closed

Bonfire: Falsy Bouncer - contradictory instructions #5303

kokushozero opened this issue Dec 15, 2015 · 3 comments

Comments

@kokushozero
Copy link

kokushozero commented Dec 15, 2015

Challenge Bonfire: Falsy Bouncer has an issue.
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36.

The instructions to Falsy Bouncer indicate that a Falsy value is of various types including NaN.
The first test requires that the function call bouncer([7, "ate", "", false, 9]) should return [7, "ate", 9]
however note that the string "ate" is NaN therefor it should not be included in the resulting array.

Therefor it seems that there is no way to pass test 1 and test 3 with the same code block, they are mutually exclusive answers.

My code:

function bouncer(arr) {

  arr = arr.filter(function(val){
    if (val === false || val === null || val === undefined || val === "" || val === 0 || isNaN(val)) return false;
    return true;
  });

  return arr;
}

bouncer([false, null, 0, NaN, undefined, ""]);
@kokushozero
Copy link
Author

kokushozero commented Dec 15, 2015

Apologies in advance if I have misunderstood something here.

@ltegman
Copy link
Member

ltegman commented Dec 15, 2015

NaN is an actual value a variable can have and it is falsey. "ate" is not a number and isNan("ate") will return true, but the value "ate" is not falsey, only the specific value of NaN is falsey. The beauty (and occasional bug source) of falsey values is that you don't need to explicitly look for them like your code does, because whenever a boolean value is needed Javascript will type coerce them to a boolean value of false. What this means is that the code for your filter function can simply be this:

arr = arr.filter(function(val){
  return val;
});

Thanks and happy coding!

@ltegman ltegman closed this as completed Dec 15, 2015
@SaintPeter
Copy link
Member

SaintPeter commented Dec 15, 2015

And, technically, you can use the Boolean function to do that for you, so the problem can be solved:
return arr.filter(Boolean), since Boolean takes truthy/falsy values and returns true or false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants