Basic Algorithm Scripting - Finders Keepers

Hi! I could passed this excercise, but was just curious to know why this solution is not correct (the console always returns “undefined”). Thanks in advanced!

Your code so far

function findElement(arr, func) {
  let num = 0;
  for (let i= 0; i <= arr.length; i++){
    num = arr[i];
    if (func(num) === true){
      return num;
    } else{
      return undefined
console.log(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/ Safari/537.36

Challenge: Basic Algorithm Scripting - Finders Keepers

Link to the challenge:

The return statement immediately stops the function, even in the middle of a loop.

1 Like

Ohh, you´re right! I´d forgotten that. Thank you!

1 Like

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