Basic Algorithm Scripting - Falsy Bouncer

Tell us what’s happening:
I know this is most likely not the nicest cleanest way to do this, but I think I could make it work.
I am not sure why arr[i] is undefined and would appreciate some external eyes on it. It is defined outside of the for loop and inside the for loop but not in the if and I am not sur why

Thanks

Your code so far

function bouncer(arr) {
  let arr1 = []; 
  let i = 0;
  let arrValue = [];
  console.log("arr[i]", arr[i]);
  console.log("length",arr.length);
  for (i; i < arr.length; i++)
    
    arrValue = !!arr[i];
    console.log("value", arrValue);
    if (arrValue == true) {
      console.log("arr[i]", arr[i]);
      arr1.push(arr[i]);
  
    }
    console.log("arr1", arr1);
    return arr1;
}

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

Challenge: Basic Algorithm Scripting - Falsy Bouncer

Link to the challenge:

This is quite interesting. What is happening is caused by the lack of curly brackets to delimit the block of code within the for loop. As such, only the next line with code (arrValue = !!arr[i];) is considered as a part of loop and executed as such. Further lines are executed when loop finishes, and at that point i === arr.length.

Thanks so much! what a dollop --’

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