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.