I thought this code was pretty clean. But obviously I wrong. I’m not sure
where I went wrong here. filter() is supposed to filter() out what you don’t want in a array right?
Your code so far
function bouncer(arr) {
return arr.filter(function(element) {
return element !== "false";
});
}
bouncer([7, "ate", "", false, 9]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.1 Safari/605.1.15
This will return only elements which explicitly do not equal the string ‘false’. Note also that return element !== false would also only return values which do not explicitly equal false.
If you are trying to determine if a value is falsy or truthy, there are other approaches.
One very useful concept is this:
let a = true;
// if a is a truthy value
if (a) { console.log("a is a truthy value") }
// if a is a falsy value
if (!a) { console.log("a is a falsy value") }
This is a shorthand for determining whether a value is truthy or falsy. You could change the assignment value of a to any value to test it. For instance a = "false" is actually true, because it’s just a string and strings are truthy.
Thanks for breaking this down into a concept I can easily get. It is best to break complex problems into simple concepts. Thanks a lot for the help man, much appreciated.