Drop it I cant get it why we are using these instead of the normal ones (arr.length,arr[i])

Tell us what’s happening:

Does anybody knows why we are using arr[0] instead of arr[i] ?
and why we created a variable if arr.length. ???
Thank you

Your code so far


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

// test here
dropElements([1, 2, 3, 4], 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/88.0.4324.104 Safari/537.36.

Challenge: Drop it

Link to the challenge:

Whoever wrote this code is choosing to modify the array as they are looping over it. That’s why arr[i] wouldn’t work.

1 Like

Hello, Can you please explain more ? Thank you for your time!!

shift() removes the first element from the array, so every time the loop runs the indices of all values in the array shift by one. arr[0] is different every time.

Note: this solution works, but it isn’t one I would recommend.

I reordered the solutions in the guide because I also do not like this solution.

1 Like

Alriiiiiiight got it !!! Thank you for your help :mechanical_arm:

1 Like

I’m glad I could help. Happy coding!

3 Likes