Finders Keepers_Help_

Tell us what’s happening:

Can’t understand why in this challenge it is asked to return 8, while 10 is also true for given function…
Is it a bug, or peculiarity of forEach()method?

Your code so far



function findElement(arr, func) {

  arr.forEach(function(element){
    if(func(element)== true){
      return element;
    } else {
      return undefined;
    }
  })


};

findElement([1, 2, 3, 4], num => num % 2 === 0);

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/basic-algorithm-scripting/finders-keepers/

Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument). If no element passes the test, return undefined.

That’s why we want to return 8, because it’s the first element to pass the test

1 Like

Ah, I overlooked it…Thanks, my bad.