Last active
July 8, 2018 13:00
-
-
Save mmloveaa/47495ce239b306b17111eccbf24f80c9 to your computer and use it in GitHub Desktop.
4-26 Free Code Camp remove all falsy
This file contains hidden or 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
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]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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, ""])