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

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
Hey everyone, am stuck on why my code cant work,please someone to review and correct me…thanks.
Link to the challenge:

Your return statement is in the wrong place. It will return newArr as soon as it finds the first truthy value, which is not what you want.

As an aside, you don’t need to explicitly convert arr[i] to a boolean value. if (arr[i]) {...} works just as well, and saves you some extra typing.

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