Basic Algorithm Scripting - Finders Keepers

Tell us what’s happening:

I’m having trouble here. I got the undefined, but I need assistance on what should go in the if condition.

Your code so far

function findElement(arr, func) {
  let num = 0;
  for (var i = 0; i < arr.length; i++) {
    if (func[i] === true) {
      num = arr[i];
    } else {
      return undefined;
    }
  }
  return num;
}

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/131.0.0.0 Safari/537.36

Challenge Information:

Basic Algorithm Scripting - Finders Keepers

Note - you should not use var

Note - this is not a function call

Here’s my edited code:

function findElement(arr, func) {
  let num = 0;
  for (let i = 0; i < arr.length; i++) {
    if (func(arr[i]) === true) {
      num = arr[i];
    } else {
      return undefined;
    }
  }
  return num;
}

Ok

But I’m still getting it wrong. I wonder what issue the func(arr[i]) is causing in the if condition.

Your problem is with the logic of your if-else body. What does a return statement do?

It’s returning undefined rather than a value. That’s the issue that I’m facing.

You did not answer my question. What does a return statement do?

A return statement returns a specified value.

That’s not all it does though.