So this code I have so far is the filter portion of the challenge where I’m supposed to return a new array which excludes the falsy values. Since I can’t use && /II operators in the filter itself to test for all the falsey values I assume I have to write some code which contains all the values to compare. I have no idea where to start, as embarrassing as that sounds in this point of the curriculum…
function bouncer(arr) {
let result = arr.filter(n=>n!=false)
return result;
}
console.log(bouncer([7, "ate", "", false, 9]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.
I tried using NaN on a and it came back falsy. But how am I supposed to fit the other 5 values into a? NaN is just one of the values to compare, and there are multiple. Do I declare a variable for every value?
Doesn’t the filter function take each element n, one by one, and test it? What does the code above (in my other post) suggest the test for ‘truthiness’ is?
This is how I’m trying to incorporate the example:
function bouncer(arr) {
let a = NaN
if (a) {
let result = arr.filter(n=>n!=a)
return result;
} else {
return arr
}
}
console.log(bouncer([7, "ate", "", false, 9]));
So I don’t need an if/else statement for this challenge? Sorry if I’m just going in circles but this challenge is proving harder for me to grasp than I imagined. I guess I’ll just keep going at it. If I don’t compare it against all the values listed in the challenge, I only compare it to one thing? And does that one thing hold that value of all 6 items in the challenge?
No, you don’t need to add an if when using a filter to solve this problem.
The big idea is that every Javascript variable will act like true or false when used in a condition, such as in an if, a loop condition, a filter, or any other place where you are used to using a test like === or !==.
The value is its own test. You aren’t checking if the value is in the truthy or falsy list of values. You are checking if the value acts like true or false.
If I can solve this challenge within what I originally posted and just adding a variable to the mix, I feel like I’ll get it eventually. I though I had to do things on top of the filter which threw me off including what isn’t clicking. I’ll try a lil harder
Passed the challenge! I got rid of the variable I was trying to compare and how I was trying to compare it in the filter. Here is the passing code
function bouncer(arr) {
let result = arr.filter(n=>n)
return result;
}
console.log(bouncer([7, "ate", "", false, 9]));
I appreciate you helping me through this one. It seems like I didn’t need to compare because when the filter goes through the array one by one it automatically removes the falsy values because JS is gonna check it when it runs? And truthy values could be anything else so it won’t be touched.
You got it. filter keeps values where the right side of the => is truthy. This could be a test like === or it can be Javascript convertingaa value or expression to true or false. Fasly values aren’t truthy, so the filter removes them.