Drop it - Solving using .ForEach

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) {

  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:

The return statement in the callback work for the callback, but forEach doesn’t do anything with the value returned from the callback.
The dropElements function always returns an empty array

Yeah thought the issue was with the return not being the return of the actual function. Is it possible to convert my for loop solution to .forEach in this case?

One way is to use at least one (possibly two) flag variables to keep track of the first index of arr where the function evaluates to true. Why? Because since the forEach iterates through all element of arr, you have to have a way to track when it first evaluates to true and only set that index one time.

In general, using a forEach is not the best method, because if you had a million array elements and the second element causes the function to evaluate to true, it is extremely inefficient to keep iterating past the second element.

A second “hack” to break out of the forEach using a try…catch, but this is ugly code and is not recommended.

I recommend sticking with traditional for loop or for in loop. You can also try Array.Prototype.findInde.