Falsy bouncer if statement

can someone help me with this, I dont understand why it is not working?

  **Your code so far**

function bouncer(arr) {
let newArr = [];
!!(false)
!!(null)
!!(0)
!!('')
!!(undefined)
!!(NaN)

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

}   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/92.0.4515.107 Safari/537.36 Edg/92.0.902.62

Challenge: Falsy Bouncer

Link to the challenge:

Is this actually the code? Because these lines do nothing:

!!(false)
!!(null)
!!(0)
!!('')
!!(undefined)
!!(NaN)

You’re then checking if each of the array values are literally true in the loop.

Is this symbol not mean to declare that those values are booleans? Or does it recognise it automatically?

So should I write if it is not false in the loop condition?

Focus your attention on this line here

Remember that if statements only execute if the condition is true.

You are overthinking it by adding the === true

It might also help to review the docs on if statements and truthy values.

Hope that helps!

2 Likes

A “truthy” value is one that is considered “true” when used as an “if”-condition and that’s it.
For all other purposes (including comparisons) they are NOT “true” or “false”. Hence comparing them via “==true” will always be false.

1 Like

As @jwilkins.oboe says, focus on the loop.

This !! is two NOTs.

One NOT flips the truth of a value

!true

Is false, for example. It works by converting whatever the value is to a boolean (true/false), then flipping it. So

!7

Is “NOT 7”. 7 is not a boolean, but it is a “truthy” value: if you convert it to a boolean it is true. And “NOT true” is false.

So putting another ! in front just flips it again:

!!true

“NOT NOT true” is true. !! isn’t a special syntax, it’s just a terse way of converting a value to a boolean, like Boolean(7). You could have as many as you like, makes no sense to do so, but:

!!!!!!!!7

Is true, it’s just “NOT NOT NOT NOT NOT NOT NOT NOT true”


So with this:

function bouncer(arr) {
  let newArr = [];
  !!(false)
  !!(null)
  !!(0)
  !!('')
  !!(undefined)
  !!(NaN)

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

You’ve just written:

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

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

Those false-es, they’re just the value false. You aren’t doing anything with them

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.