Finders Keepers Question

Here is my code :

function findElement(arr, func) {


  for(let i=0;i<arr.length;i++){
    let num=arr[i];
    //console.log(num);
    if(func===true){ //I think this part is not correct
      return arr[i];
    }
  }

  
}

console.log(findElement([1, 2, 3, 4], num => num % 2 === 0));

So, my thought process is this,

  1. assign each arr elements into num so that func knows what to use
  2. if func returns true, it will return the exact arr[i] then the for loop ends.

Is my logic correct?
If it is, how to write code to check whether func returns true?
If it is not, which part of it is wrong?

function_name();

??

or should I use function.prototype.call ?

function_name(2);

provided that there is only one variable in the function

so in order to check whether the function returns true, you just need to execute it?

Yes, but just make sure you pass num as the argument to the function.

1 Like

i see. thanks.

also how do I add spoiler too multiple lines of code?
i just add the spoiler tag every line or ?