I didn’t understand this solution entirely. Isn’t num already an arrow function? ( num => num % 2 === 0) . So why do we have to put it inside func? Why can’t we just write
if (func) return func;
or sth like that ? I got really confused there.
**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)) return num;
}
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:97.0) Gecko/20100101 Firefox/97.0
num is what we’ve chosen to call the first parameter in our arrow function. The arrow function doesn’t have a name. It is anonymous. This is the arrow function:
Sorry for the late response and thank you for your answer. I got really confused because “num” is named several times in the challenge and in my opinion it’s making it difficult to understand. Now I understood better.
Thanks.
Yes, there are two variables called “num”, one is in the function findElement and the other is in the anonymous arrow function. To JS there is no issue because they are in two different scopes. In terms of readability, it make sense because they are basically referring to the same value, if you trace through how the code works, But it would still work if you changed the name of the variable in one of the functions to “elephantPajamas”.