Falsy Bouncer kluge coding
See original GitHub issueChallenge Falsy Bouncer has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:48.0) Gecko/20100101 Firefox/48.0
.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
function bouncer(arr) {
// Don't show a false ID to this bouncer.
function notFalse (item) {
var x = typeof(item);
// console.log(" item " + item + " x-typeof " + x);
// if (x === "number")
// if (!item)
// item =0; // force NaN to be 0
if (item === false) {
return false;
} else if (item === "") {
return false;
} else if (item === null) {
return false;
} else if (item === 0) {
return false;
} else if (item === undefined) {
return false;
} else if (x === "number" && isNaN(item)) { // "a" would fail this test without === number
return false;
} else {
return true;
}
}
var newArr = arr.filter(notFalse);
return newArr;
}
bouncer([7, "ate", null, false, 9]);
My comments:
This was a challenging exercise. I have read the other comments in this forum and understand that this is a borderline bug/quirk of JS coding that is unlikely to change.
The “falsy” concept is not monolithic when it comes to testing for all the possible falsy values. NaN is the exception and should only be applied when the typeof (item) === “number” whereas the other falsy concepts can be tested regardless of object type.
I wonder if the typeof () function should be added to the the helpful links on this challenge?
I also wonder if it would not be better to test for the falsy concepts AFTER determining the object type, for all falsy results?
Main issue here is that the function isNaN(x) will return true for any string/character values tested (probably boolean as well but my code tests for the boolean falsy prior to the NaN test).
I know I should likely be using the chat/help function for this and while new at freecodecamp I expect to be up and running on github shortly.
Issue Analytics
- State:
- Created 7 years ago
- Comments:15 (9 by maintainers)
Top GitHub Comments
@codeman869 I like it! I think “first” could be omitted, but I like the balance between pointing the camper in the right direction, but not telling them specifically which tool they should use. Do you want to create a PR for this?
@codeman869 Good points!
I actually used your method as well when initially playing around with possible solutions, before taking a look at the link.
I do agree that maybe this is pointing to a specific solution too much. If we add your link, we’re pointing to two solutions, which could be very confusing. As a camper, I would think that I would have to do something with both links.
The page on logical operators also doesn’t really show how to convert a value to a boolean. It does state
but I wouldn’t be able to tell that it was converted to a boolean if I didn’t know already.