Finders Keepers works in console but not on site

Code works fine in console (https://repl.it/@cubefox/-2) but somehow on freecodecamp my answer gets rejected… any Ideas why?

Your code so far


function findElement(arr, func) {
  for(i of arr){ if(func(i)) return i; }
  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; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

You need to use the var or let to define i in your for loop.

As I understand it when you do not use var/let the variable becomes global and not local. There might be something in the challenge that either stops global variables from being allowed or overrides them, but in any case it is good practice to always define variables using var (or let etc).

Hope this helps :slight_smile:

1 Like