Implement an Element Skipper - Implement an Element Skipper

Tell us what’s happening:

My code is failing tests 4 & 7. I’ve tried figuring out what is causing the problem, but I can’t figure out the connection. I’m really not sure why these two particular tests are failing. I’m sure it’s just something obvious I’m missing. Any help would be greatly appreciated.

Your code so far

function dropElements(arr, func) {
  let arrEnd = arr.length;
  let newArr = [];
  for (let i = 0; i < arr.length - 1; i++) {
    if (func(arr[i])) {
      newArr = arr.slice(i, arrEnd);
    }   
  } 
  return newArr; 
}

console.log(`Original array: [1, 2, 3, 4]`);
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}));
console.log(`Should return: [3, 4]
`);

console.log(`Original array: [0, 1, 0, 1]`);
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}));
console.log(`Should return: [1, 0, 1]
`);

console.log(`Original array: [1, 2, 3]`);
console.log(dropElements([1, 2, 3], function(n) {return n > 0;}));
console.log(`Should return: [1, 2, 3]
`);

console.log(`Original array: [1, 2, 3, 4]`);
console.log(dropElements([1, 2, 3, 4], function(n) {return n > 5;}));
console.log(`Should return: []
`);

console.log(`Original array: [1, 2, 3, 7, 4]`);
console.log(dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}));
console.log(`Should return: [7, 4]
`);

console.log(`Original array: [1, 2, 3, 9, 2]`);
console.log(dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}));
console.log(`Should return: [3, 9, 2]
`);

Your browser information:

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

Challenge Information:

Implement an Element Skipper - Implement an Element Skipper

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-element-skipper/a5deed1811a43193f9f1c841.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @bobiswrong,

Will i ever reference the last array element?

Try adding a print inside this if statement to see what arr.slice(i, arrEnd) is at this point.

Happy coding

@dhess

Thank you so much! That helped me to figure out the problem, and it passed. I appreciate the help.