Don't understand how to use functions in this context

Tell us what’s happening:
Describe your issue in detail here.
Hi everyone. So I’m trying to work in this problem but I really don’t understand how to pass an array into a function that is an argument. I appreciate any help, thanks.

  **Your code so far**

function findElement(arr, func) {
let num = 0;
for(let i = 0; i < arr.length; i++) {
 num = arr[i];
 if (num )
}
return num;
}

console.log(findElement([1, 2, 3, 4], num => num % 2 === 0));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36

Challenge: Finders Keepers

Link to the challenge:

2 Likes

The tricky thing to understand is how javascript sees functions. You see a function, but javascript sees data. Functions are treated like any other data, placed in variables or arrays, and stored in memory where we can refer to it later.

When we pass a function in like this, we’re sending a reference to that same memory location - but it’s still just data to javascript. Unless we call it as a function, javascript lets us pass it around, like any other data.

So within our function, we’ve placed that reference to the function in a parameter called func. We don’t know it’s original name, and we don’t need to. Within the scope of our outer function, we refer to that passed function as func.

And because we see it is a function, we can call it as a function.

1 Like

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