How works .shift() in this code?

Why does it work with times but not just arr.length?
This is one of the solutions from FCC.

function dropElements(arr, func) {

var times = arr.length
for(var i=0; i<times;i++){
return arr


dropElements([1, 2, 3, 4], function(n) {return n > 5;})
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})
  **Your browser information:**

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

Challenge: Drop it

Link to the challenge:

If your original array starts with 5 items and you call shift() on it, how many items does it have after the shift? And how will that affect the expression i < arr.length in the for loop?

The length of the array is shrinking, so arr.length is decreasing on each iteration. This means you may undershoot the original length of the array.

Honestly, this isn’t my favourite solution. I think it’s a better practice and cleaner to find the index of the first element that passes the func test and return everything from that element onward with a slice.

1 Like

Yes, I am sorry if I didn’t make myself understood.