Finders Keepers (ternary operator)

What mistake here? In the usual if everything worked, but here the first condition is not accepted.

function findElement(arr, func) {
let num = 0;
for (let i = 0; i < arr.length; i++) {
  num = arr[i];
  return func(num) ? num : undefined
findElement([1, 2, 3, 4], num => num % 2 === 0);

Challenge: Finders Keepers

Link to the challenge:

The function stops the very first time it encounters a return statement.

1 Like

does it mean that the ternary operator cannot be used inside functions?

You can certainly use a ternary inside of functions. I do it all the time. But you need to only use return when you want to exit the function.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.