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.
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.