Finders Keepers works in console but not on FCC

Tell us what’s happening:

Your code so far


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

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

FCC forces you to use the num variable as they’ve initialized even though my solution here works perfectly in console without it.
Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Link to the challenge:
javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers]

It has something to do with the way babel is transforming it. If you add the { } to enclose your for loop code it will work.

It is typically better to use brackets to clarify your intent with JavaScript. If this was Python, then it would be different.

1 Like