Finders Keepers (question about arrow functions)

Tell us what’s happening:
So, I actually solved this one quite easily. But investigating around arrow functions, I have a maybe concept problem with arrow functions or functions in general.
So:
if I refer to “func” and I pass the argument “num”, it executes the function and it works, perfect, problem solved.
But as you see in my code, I was trying to store into an array the result of the function (num%2 without the ==0). And it stores the function per se. this is the console.log output:

(4) [ƒ, ƒ, ƒ, ƒ]
0: ƒ (num)1: ƒ (num)2: ƒ (num)3: ƒ (num)
length: 4__proto__: Array(0)

So basically the question is, how do I successfully pass the “num” to my function inside push?

thanks guys

Your code so far


function findElement(arr, func) {
  //fiddling with arrow function
  
  let resMod=[];
  for (let num of arr){
   resMod.push( num => num % 2);
  }
  console.log(resMod);

  //actual solving of the exercise
  /*
  for (let num of arr){
   if (func (num) == true){
     return num;
   }
  }
  return undefined;
  */

}


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


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36.

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

As you showed above this pushes the un-evaluated function itself into your array, the result of the function is however obtained with func(num), but since func(num) returns a boolean you have to set a conditional to test if it is true before you push num, so

   if(func(num)){
     resMod.push( num);
   }
1 Like

You need to use this syntax to pass num into the arrow function and execute it.
resMod.push((x => x % 2)(num));

1 Like

Thanks, this is what I needed!