Tell us what’s happening:
hello everyone, I would like to ask some questions here. below is the code from the solutions from the hints.
my first question is why do we need to add
let originalLen = arr.length
in the code?
I tried to remove it and use
for (let i = 0; i < arr.length; i++)
instead but it didn’t let me pass one test which is
dropElements([1, 2, 3, 4], function(n) {return n > 5;}) should return [] .
could someone please explain why do we need to add let originalLen code and why it won’t pass one of the test if we didn’t add the let originalLen?
second question is
why is the if function use arr[0] instead of arr[i] i tried to change the 0 to i and it only pass 2 out of 6 test.
as far as my understanding arr[0] will only shows the first item in the array while arr[i] will reiterate each item in the array which what we needs.
what did i miss here?
Your code so far
function dropElements(arr, func) {
let originalLen = arr.length;
for (let i = 0; i < originalLen; i++) {
if (func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}
console.log(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/107.0.0.0 Safari/537.36
Challenge: Intermediate Algorithm Scripting - Drop it
Link to the challenge: