function bouncer(arr) {
function checkFalsy (value) { // 'value' is what is being tested
// check if the value is 1. a number, OR 2. a boolean
// 'true' variables are either a boolean 'true', or a number.
if (typeof(value) === 'number' || true ) {
return value; //if the value passes test return it.
}
}
// filter takes every element in this array,
// and tests it in the above function.
// If the element in array passes the test above,
// it is returned like previously stated.
// only 'true values which pass the test are returned'
// you now have an array with only 'true' values.
return arr.filter(checkFalsy);
}
bouncer([7, "ate", "", false, 9]);
I normally would not post a solution, but seeing how the OP had already posted his working one, I don’t see any reason for not posting a piece of code that was easier to understand which is what he was looking for.
Yeah, that’s why I was thinking the spoiler alert type tag, so people could discuss but people still working on the problem didn’t get their fun wrecked. Not a big deal.
As the condition in the if is alwaystrue, the code
function checkFalsy (value) {
if (typeof(value) === 'number' || true ) {
return value;
}
}
is same as
function checkFalsy (value) {
return value;
}
(the if is just there for distraction)
filter then calls the callback for every element, and whenever it receives a truthy value, it will include that element in the returned array.
Please note that the returned array contains the original array’s elements, not the callback results, these are only evaluated on whether they are truthy or falsy!
I kind of stumbled on the solution to this problem on my own. I also didn’t understand how it worked, which lead me to this forum thread. I ended up with this code:
function bouncer(arr) {
function isNotTrue(value) {
return value;
}
return arr.filter(isNotTrue);
}
But then after reading this thread I changed it to the code below and it also allowed me to pass the tests. My question is; Which solution is the better way of doing it? And why?
function bouncer(arr) {
return arr.filter(isNotTrue);
}
I just completed this challenge and was curious to see other solutions. I actually used the Boolean object as instructed and didn’t work my way around it as initially tempted. It took a good half hour to wrap my head around the concept of the Boolean object.
My solution
function bouncer(arr) {
function check(x) {
return Boolean(x);
}
var newArr = arr.filter(check);
return newArr;
}
bouncer([7, "ate", "", false, 9]);
This is exactly what I did. I also felt like the solution needed to include the Boolean object since it was referenced. (though I don’t always follow the hints and I’ve been known to solve the challenge in a different way). It also took me quite a while to get my head around the code. I kept re-reading the MDN pages on Boolean and .Filter and each time I felt like I understood one more tiny amount.
I stared at my code for a while trying to figure out why it wasn’t working and finally I put the “return” in front of the Boolean(arr) statement in my function and presto, it passed. I was thinking that I didn’t need to “return” a boolean statement, and that it did that automatically. I guess not.
Here’s my code, pretty much identical to Soupedenuit above:
function bouncer(arr) {
var newArr = [];
function checkWord(arr) {
return Boolean(arr);
}
newArr = arr.filter(checkWord);
return newArr;
}
bouncer([7, "ate", "", false, 9]);