Drop it help | Drop it help

Tell us what’s happening:
This code passes all the tests except :

dropElements([0, 1, 0, 1], function(n) {return n === 1;}).

It keeps slicing the array one index off from the rest. I don’t understand why.

  **Your code so far**
function dropElements(arr, func) {
let anw = "";
//console.log(func(arr[1]))
for(let i = 0; i < arr.length; i++) {
//console.log(i);
//console.log(arr[i]);
if(func(arr[i])) {
  return arr.slice(arr[i-1]);
};
};
return [];
}

console.log(dropElements([1, 2, 3], function(n) {return n < 3; }));
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}))
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}));
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14695.85.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Drop it

Link to the challenge:

You got lucky you were passing any of the tests.

Remember the slice method takes a starting index as the first argument. For some reason you have:

return arr.slice(arr[i-1]);

This is going to attempt to return a slice of arr starting at whatever arr[i - 1] is. That is just a value of an array element and has nothing to do with the necessary index.

1 Like