Finders Keepers - Works with For not with ForEach

It works with for loop, then what is the problem with forEach?

function findElement(arr, func) {
  arr.forEach(x => {
    if (func(x))
      return x;
  return undefined;

findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0);

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

Challenge: Finders Keepers

Link to the challenge:

this is the only return statement in your findElement function, also forEach returns undefined on its own so you can’t even just return the forEach function

So could there be a solution with forEach?

yes, forEach can be used, but if you want to use an array method, you can do some research and find a more approrpiate method

Alright, Thank you. I will find some more appropriate and optimal methods.

Let me know when you see this once.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.