Tell us what’s happening:
It’s returning the undefined case, but not any numbers from the array.
Your code so far
function findElement(arr, func) {
let num = 0;
for (var i = 0; i < arr.length; i++){
if (func(arr[i])){
return num = arr[i];
}else{
return undefined;
}
}
}
findElement([1, 2, 3, 4], num => num % 2 === 0);
That is because your for loop only makes it one iteration before you return something. You only should return undefined if no element passes the test. Your function returns undefined if the very first element does not pass the test. Remember, when the return statement is executed, the function is exited and does not come back.
the function findElement is defined with two parameters, arr and func
instructions tell you that the first will always be an array and the second will always be a function
this is a function call:
the function is called with some arguments.
the first one is an array, [1, 2, 3, 4], the second one is a function, num => num % 2 === 0
is the function your issue? that function is defined using arrow function, a short hand introduced in ES6