Problem in Drop it Algorithm

Hy, I have all the tests in the challenge checked except

dropElements([1, 2, 3, 4], function(n) {return n > 5;})

My code is as following:

function dropElements(arr, func) {
  // Drop them elements.m
  
  for (var i = 0; i < arr.length; i++) {
    if (func(arr[0])) break;
    arr.shift();
  }
  
  return arr;
}

This is giving me a headache, it just doesn’t make any sense.
Thanks for the help

This is a good example of why you don’t want to modify an array that you are iterating over. I’m going to try to break down the sequence of events here.

  • Enter for loop for the first time:
    • arr is [1,2,3,4]
    • arr.length is 4
    • i is 0
    • 0 < 4, so execute the loop block
  • if statement:
    • arr[0] is 1
    • 1 < 5, so func returns false
  • shift
  • Second iteration of for loop:
    • arr is [2,3,4]
    • arr.length is 3
    • i is 1
    • 1 < 3, so execute the loop block
  • if statement:
    • arr[0] is 2
    • 2 < 5, so func returns false
  • shift
  • Third iteration of for loop:
    • arr is [3,4]
    • arr.length is 2
    • i is 2
    • 2 < 2 is false, so exit the loop
  • return

Thank you so much, so the array’s length changes throughout the iterations, I solved the problem by changing the condition to:
arr.length !== 0
but I’ll try to avoid changing the array as I’m looping through it next time.
Thanks again.