Skip to content

Instantly share code, notes, and snippets.

@joePichardo
Created December 10, 2015 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joePichardo/86992e16ee49c69e141a to your computer and use it in GitHub Desktop.
Save joePichardo/86992e16ee49c69e141a to your computer and use it in GitHub Desktop.
Remove all falsy values from an array. Falsy values in javascript are false, null, 0, "", undefined, and NaN.
// 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