Drop it exercise

Tell us what’s happening:
why does this program does’nt satisfy the 4th problem

Your code so far


function dropElements(arr, func) {
for(let i = 0; i < arr.length; i++){
  if(func(arr[0])){
    break;
  }else {
    arr.shift()
  }
}


return arr;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36.

Challenge: Drop it

Link to the challenge:

The length of the array is changing as you loop, which means your loop does not do what you expect.

Just declare a variable with initial length of the arr,
and run for loop with that variable

As the arr is changing, your loop is terminating without traversing whole arr

1 Like

Though, the fact that the for loop iterator variable i is never used is an indication that a for loop is not the best fit for this challenge. You can get a shorter code and simpler logic with a while loop.

1 Like