Ok, thanks Dan I fixed it, now it is passing all the tests. THank you!
However can you please explain to me the following:
function findElement(arr, func) {
let num= undefined;
for (var i=0; i<arr.length; i++){
num=arr[i];
if (func(arr[i])){
return num;
}
}
return num;
}
console.log(findElement([1,4,2,3, 5, 9,7], function(num) { return num % 2 === 0; }));
The only difference in the above is I took num=arr[i]; and I placed it before the if statement. This will return the first even number however, if there are no even numbers it will still return the last number of the array regardless of if its even or odd. I realize this is incorrect, but can you please explain why does it do this?