Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active July 8, 2018 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmloveaa/47495ce239b306b17111eccbf24f80c9 to your computer and use it in GitHub Desktop.
Save mmloveaa/47495ce239b306b17111eccbf24f80c9 to your computer and use it in GitHub Desktop.
4-26 Free Code Camp remove all falsy
Remove all falsy values from an array.
Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
Boolean Objects
Array.filter()
function bouncer(arr) {
// Don't show a false ID to this bouncer.
return arr.filter( function( value ){
return value ? true : false;
});
}
bouncer([7, "ate", "", false, 9]);
// other solution:
function bouncer(arr) {
var truths = arr.filter(function(filterTrue) {
return filterTrue;
});
return truths;
}
bouncer([7, "ate", "", false, 9]);
@leolikestocode
Copy link

Another solution =)

function bouncer(arr) {
for (var i =0; i<arr.length; i++){
if (!arr[i]){
arr.splice(i,1);
i--;
}
}
return arr;
}

bouncer([false, null, 0, NaN, undefined, ""])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment