Drop it: why this code is not working

Drop it: why this code is not working
0.0 0

#1

Tell us what’s happening:

Your code so far


function dropElements(arr, func) {

for(let i =0; i <= arr.length; i++){
       if(!func(arr[0])){
   arr.shift();
  }
  
}
  return arr;
}


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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it/


#2

You have to use Array.splice() to remove the first element from an array.

Here is your code:

function dropElements(arr, func) {
  while(!func(arr[0])) arr.splice(0, 1);
  return arr;
}

dropElements([1, 2, 3], function(n) {return n < 3; });

#3

You do not have to use splice to solve this challenge. Also, please do not post full working solutions to the challenges. Give the OP hints or suggestions, but not full solutions. Your solution has been blurred out to prevent spoiling the solution for someone who comes to this thread looking for hints and suggestions and not a full solution.


#4

@randelldawson
Oh I didn’t knew that. Thanks!