Finders Keepers challenge --- Isn't num already an arrow function?


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

Challenge: Finders Keepers

Link to the challenge:

Why do you think num is an arrow function?

I thought that’s how an arrow function is written.

That doesn’t make num an arrow function.

then what does => referring to?

input => 2 * input /* output */

those are just local variable names


Thanks a lot for the quick reply and explanation.

1 Like

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:

num => num % 2 === 0
1 Like

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.

1 Like

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”.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.