I have already solved this challenge with the code at the bottom of this post.
Question is, how can I ‘‘convert’’ the code so that it works with .ForEach? I have the following so far:
function dropElements(arr, func) {
arr.forEach(n=>{
if (func(n)) {
return arr.slice(arr.indexOf(n))
}
})
return []
I think the return statement doesn’t mean it is a return statement for the function itself which is the issue.
Your code so far
function dropElements(arr, func) {
for (let i = 0 ; i<arr.length ; i ++) {
if (func(arr[i])){
return arr.slice(i)
}
}
return [];
}
dropElements([1, 2, 3], 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/73.0.3683.103 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it/