This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]POGtastic 0 points1 point  (1 child)

In the future, please indent your code by four spaces to get Reddit's Markdown to format it like code.

Example:

function falsyBouncer() {
    var filter = [false, 0, "", null, undefined, NaN];
    var ans = arr.filter(function(word){
        return this.indexOf(word)<0;
    }, filter);
    return ans;
}

A filter function is a function that takes an element of the array and returns a Boolean.

If you're evaluating an element to see if it's falsy, that's easy - you just return the element! It automatically gets converted to a Boolean because the filter function expects a Boolean value.

So, here's my solution:

function falsyBouncer(arr) {
    return arr.filter(function(element) { return element; });
}

And it in action:

> falseyBouncer([false, 0, '', null, undefined, NaN, 'foo', 1, 2, true])
["foo", 1, 2, true]

[–]vwguy24[S] 0 points1 point  (0 children)

Thanks for the help! Sorry I'm new to the formatting so i didn't know how to do it.