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,
- assign each arr elements into num so that
func
knows what to use - 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?