Basic Algorithm Scripting - Falsy Bouncer

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

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

bouncer([7, "ate", "", false, 9]);*/
function bouncer(arr) {
  const filteredArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]) filteredArr.push(arr[i]);
  }
  return filteredArr;
}

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

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

I do know that my commented out solution works so no problem with that.
I just cant wrap my head around how the sample solution checks if the values in array are falsy or not.
I do realize it happens in if statements (arr[i]) part but I don’t have the slightest idea how.
Does it call for some function that is out of our sight?
Is there some built-in functionality to check for booleans?
What is going on in the brackets? That makes no sense to me.

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

If statement checks the condition and in this case the condition is arr[i]

So long as arr[i] is not a zero or any of the other falsy values, then the if will fire as anything other than those values is considered to be true.

what are you looking for is Boolean coercion

So you are saying that (arr[i]) is read as true or false depending what is stored in the [i].
For example, if (arr[i]) that has 1 stored in [i] would be read as if I had typed in if (true)? Following the same logic all the falsy values would be read as if (false).

Am I on the right track or?

Yes you are on the right track.

yes basically when the value in arr[i] is used in a contest when a boolean was expected, it is converted to a boolean, it can be a truthy or falsy value

I want to believe I got it down now.
Thank you a ton for both of you guys, especially you @dball!

2 Likes

ohh don’t worry it takes time for these stuff to sink in, anyway you are welcomed :+1:

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