Basic Algorithm Scripting - Finders Keepers Syntax Confusion

I’m confused at the line findElement([1, 2, 3, 4], num => num % 2 === 0); I don’t understand the syntax for the second argument - is this arrow syntax? What is this code saying? I can still probably fix the code, but I want to understand what I’m doing.

  **Your code so far**
function findElement(arr, func) {
let num = 0;
return num;
}

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/105.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Finders Keepers

Link to the challenge:

Hello!

Yes, that’s correct.

The arrow function supplied as the second parameter will receive a parameter called num that will return a true or false depending on the condition: num % 2 === 0 (the condition checks if a number is even or not: true when even, false otherwise).

The same code could be rewritten like this:

// The second parameter, in this case, is called an anonymous function
findElement([1, 2, 3, 4], function (num) {
  return num % 2 === 0;
});

And the previous code is the same as this:

function theConditionApplied(num) {
  return num % 2 === 0;
}

// In this case, you pass a reference to the function as the second parameter
findElement([1, 2, 3, 4], theConditionApplied);

Does it help :slight_smile:?

Oh, I think I see. I only learned about arrow functions recently, so I don’t entirely know how they work yet - I just didn’t think about how they can be used without being assigned to a variable. Just to make sure, this code does the same thing, right?

const condition = num => num % 2 === 0;
// same as condition = function(num) { return num % 2 === 0; }

findElement([1, 2, 3, 4], condition);

If so, I think that clears everything up for me, thanks so much!

Yes, that would be the same :slight_smile:.