Remove all falsy values from an array. Falsy values in javascript are false, null, 0, "", undefined, and NaN.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Falsy Bouncer | |
// Author: @joepichardo | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function bouncer(arr) { | |
// Don't show a false ID to this bouncer. | |
function noFalsyVals(value){ | |
//function used in conjunction with filter() function | |
//compares falsy values and returns value if none are found | |
//else it returns nothing | |
if (value !== false || value !== null || value !== undefined || value.isNaN() === false){ | |
return value; | |
}else{ | |
return; | |
} | |
} | |
arr = arr.filter(noFalsyVals); | |
return arr; | |
} | |
bouncer([7, "ate", "", false, 9]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment