Falsy Bouncer (Help Question)

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.

Challenge: Falsy Bouncer

Link to the challenge:

Right idea, but not quite the right execution.

As a hint, what does this code do?

let a = 5;
if (a)
  console.log("This was truthy");
else
  console.log("This was falsy");

Run this code and see what happens. Try some other values of a. Try a = NaN.

1 Like

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?

1 Like

The test for truthiness is if(a)?

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]));

Right.

A variable is ‘truthy’ if when you use the variable as a logical condition, such as in an if statement, the value ‘acts’ like true.

A variable is ‘falsy’ if when you use the variable as a logical condition, such as in an if statement, the value ‘acts’ like false.

There is no special test. The value itself is the test for truthiness.

See also


1 Like

Hmm, that’s not what I meant. Sorry for the confusion.

I did not mean for you to insert if (a) into your code. I was trying to show you how a variablecan evaluate as true or false without a comparison.

You cannot check n != someFalsyValue because you can’t compare against every falsy value.

1 Like

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.

1 Like

This is, by the way, a pretty tricky idea until it ‘clicks’ for you. Struggling is normal.

1 Like

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

There is no special extra variable to add. You have 7 characters too many in your original code.

You don’t need to compare your array elements to anything. They are truthy or falsy on their own.

1 Like

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.

2 Likes

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.

Nice job working it out!

2 Likes