Finders Keepers Question

Finders Keepers Question
0

#1

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?


#2

func is a function. How you you call a function and pass it a value?


#3
function_name();

??


#4

or should I use function.prototype.call ?


#5

Yes, but if I want to pass a value to the function like the number 2 or a variable, how would you do that?


#6

function_name(2);

provided that there is only one variable in the function


#7

Yes, so in the challenge you are supposed to call func using num as it’s argument.


#8

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


#9

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


#10

i see. thanks.

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


#11

No, you just put the opening tag on a separate line and put all the lines of code you want after it and then put the closing tag below the code you want to hide.

[spoiler]

// line of code
// line of code
// line of code

[/spoiler]