Falsy Bouncer (Spoiler)

Falsy Bouncer (Spoiler)
0

#1

I was struggling with this challenge for long and didn’t know how to filter all falsy values, but it was too simple if we use Boolean(value) method/function. Just sharing for the people who are struggling with this challenge. Hope it helps!

function bouncer(arr) {
    var newArray = [];

    newArray = arr.filter(function(value){
        return Boolean(value)=== true;
    });
    return newArray;
}

bouncer([7, "ate", "", false, 9]);

#2

I know. You don’t even need to type === true .

It can be as small as:

return arr.filter(item => item);

Also

var x = 0;
var y = 10;
console.log(!x) // logs true, meaning x is false
console.log(!y) // logs false

#3

I cleaned up (and add spoiler tags to) your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#4

Thanks for the feedback!


#5

Appreciate your feedback, thanks.


#6

Hey fuqted, haha, do you have some time to explain the logic in the filter prototype called in your code?

I understand that you are calling the array and filtering by the items, but what does it mean when you compare the item to be greater than or equal to the item?

Thank You!


#7

@fuqted’s code is written in ES2015 and uses an Arrow Function.

=> is not a comparison of greater than or equal to – that would be >=

You can read more about Arrow functions at:
http://exploringjs.com/es6/ch_arrow-functions.html


#8

Thank you, Bill Sourour. I’m reading the arrow functions lesson you sent now. I appreciate your help!