Skip to content

Instantly share code, notes, and snippets.

  • Save dvaun/2ea6ba705b8bdddc30dd to your computer and use it in GitHub Desktop.
Save dvaun/2ea6ba705b8bdddc30dd to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/dvaun 's solution for Bonfire: Falsy Bouncer
// Bonfire: Falsy Bouncer
// Author: @dvaun
// Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer?solution=function%20bouncer(arr)%20%7B%0A%20%20%2F%2F%20Don%27t%20show%20a%20false%20ID%20to%20this%20bouncer.%0A%20%20function%20list(obj)%20%7B%0A%20%20%20%20%20%20if%20(typeof(obj)%20%3D%3D%20%27number%27%20%26%26%20!isNaN(obj))%20%7B%0A%20%20%20%20%20%20%20%20%20%20if%20(obj%20!%3D%3D%200)%20%7B%0A%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%20else%20if%20(typeof(obj)%20%3D%3D%20%27string%27%20%26%26%20obj.length%20%3E%200)%7B%0A%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%7D%20else%20%7Breturn%20false%7D%0A%20%20%7D%0A%20%20var%20arrCheck%20%3D%20arr.filter(list)%3B%0A%20%20console.log(arrCheck)%3B%0A%20%20return%20arrCheck%3B%0A%7D%0Abouncer(%5B7%2C%20%22ate%22%2C%20%22%22%2C%20false%2C%209%5D)%3B%20%2F%2F%20should%20return%20%5B7%2C%20%22ate%22%2C%209%5D.%0Abouncer(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%5D)%3B%20%2F%2F%20should%20return%20%5B%22a%22%2C%20%22b%22%2C%20%22c%22%5D.%0Abouncer(%5Bfalse%2C%20null%2C%200%2C%20NaN%2C%20undefined%2C%20%22%22%5D)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function bouncer(arr) {
// Don't show a false ID to this bouncer.
function list(obj) {
if (typeof(obj) == 'number' && !isNaN(obj)) {
if (obj !== 0) {
return true;
}
} else if (typeof(obj) == 'string' && obj.length > 0){
return true;
} else {return false}
}
var arrCheck = arr.filter(list);
console.log(arrCheck);
return arrCheck;
}
bouncer([7, "ate", "", false, 9]); // should return [7, "ate", 9].
bouncer(["a", "b", "c"]); // should return ["a", "b", "c"].
bouncer([false, null, 0, NaN, undefined, ""]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment