[spoiler] Finders Keepers

Tell us what’s happening:
This is my work so far and it passed, but it’s kind of a cheat since on line 6 I directly used (num % 2 === 0) as the if statement condition. I tried to change this condition to if (func) or if (func == true) but it would not pass.

I thought (func) would generate the same result as (num % 2 === 0) because it is literally the only code in the func function, but it wouldn’t, and I’m confused why not. Can anyone please explain to me? Thank you very much.

Your code so far

[spoiler]
function findElement(arr, func) {
  let x = 0;
  for (let i=0; i<arr.length; i++){
    let num = arr[i];
    console.log(num);
    if (num % 2 === 0){
      return arr[i];
      i = arr.length;
      x++;
    }
  }
  if (x == 0){
    return undefined;
  }
}

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

Your browser information:

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

Link to the challenge:

if (func(num)) should work.

I tried this it didn’t work…

see below

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

You are not returning the first number that pass the function, but the last one. Think of where your return is and what num Is at every iteration of the for loop

Thanks for checking out… i found my issue a bit later ! Obvious !! :slight_smile: