Tell us what’s happening:
Describe your issue in detail here.
The return value of the conditional operation is returning 8 twice for some reason. How do I return the first true value of the number used in the function once?
**Your code so far**
function findElement(arr, func) {
let num = 0;
for (let i=0; arr.length; i++){
num = arr[i];
//console.log(num);
/// console.log(func(num));
return func(num) == true ? num : undefined;
}
}
console.log(findElement([1, 3, 5, 8, 9, 10], function(num) { return 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/92.0.4515.159 Safari/537.36
A ternary here is not the right tool because you always return something which stops your loop. You only want to stop the loop if the function returns true.
I’m not seeing how that happens with the code you posted. Your code returns undefined when it checks the first element in the array which is 1.
thanks for spotting the error, I fixed it ((let i=0; i<arr.length; i++).
Thanks eoja, This is the answer I was looking for, it makes sense, as I am returning a value at the end of each iteration, rather than only returning the value once the condition is met.