Falsy element exercise

Tell us what’s happening:
Describe your issue in detail here.
im not able to get this to compile i followed all the suggestions of the hint. what am i doing wrong here

  **Your code so far**

function bouncer(arr) {
for (let i = 0; i<arr.length; i++){
  Boolean(arr[i]);
  console.log(arr[i])
  
}
arr.filter(arr=>true);
return arr
}

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/91.0.4472.164 Safari/537.36

Challenge: Falsy Bouncer

Link to the challenge:

What does this do?

What does this do?

I don’t think that you understand what these are doing.

What do you think they might do? What do you want them to do?

i interepret it as keeping whatever element is true and filtering the rest

For which part? I highlighted two parts of the code.

the first one changes each elemment to boolean (true or false) . which the hint suggested doing . and the second code filters out and removes the false elements.

The first part doesn’t change anything. And you should not change the elements to true or false anyways. You need to keep the original values of each element.

The second part doesn’t filter anything. The filter() method for an array takes a function as it’s argument, and that function should return true for elements you want to keep and false for elements you want to reject.

And the filter() method does not change the original array, it makes a new array.

ok thanks, guess i got to make a few changes to my approach.

You have all of the right pieces, they just don’t work the way that you think they do. I’d look at examples of how filter() works.

Tell us what’s happening:
Describe your issue in detail here.
is my iff statement correct?

if (arr[i]===true){

what i want to do is iterate through the loop checking to see if the element is true.if the test is true , go on to the next statement.

  **Your code so far**

function bouncer(arr) {
let newArr=[];

  for (let i = 0; i<arr.length; i++){
    if (arr[i]===true){
      newArr.push(arr[i]);
      console.log(newArr)
    }
 
}
return newArr
}

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/91.0.4472.164 Safari/537.36

Challenge: Falsy Bouncer

Link to the challenge:

Right, but remember that this isn’t a “true accepter” but a “falsy bouncer”. Yes, you’re approach of accepting the opposite of falsy is a good idea, the problem is that the opposite of falsy isn’t “true”, it’s “truthy”. Do you understand the difference? How do you test for truthy?

oh i see. i must change the statement to truthy

i came across an example in mozilla and supposedly if u remove the equal sign and everything on the right of it , the if statement test for truthy. and i tried that on my code followed by a console.log statement to verify and i get the right result.

Yes, that is 100% correct. The if technically tests for thruthy/falsy. That is a very common pattern.

Good work.

1 Like