Timing of returns, a query related to falsy value undefined

Tell us what’s happening:
Describe your issue in detail here.
I passed the test, but I initially I wasn’t when I returned undefined in the scope of the else command. I moved it to the end of the function and it worked. My idea is that a falsy value like undefined requires the entire completion of the function to be valid. (If a function is still active then an action in the function cannot be falsy, it could still be inherently truthy). However, I don’t feel confident in this statement and would like a more experienced hand to explain to me why undefined (at least in this particular problem but also in general if applicable) needs to be at the end of the function.

Thank you!

  **Your code so far**

function findElement(arr, func) {
let num = 0;
for(let i = 0; i < arr.length; i++){
  num = arr[i];
  if(func(num)){
    return num;
  } else {
    
  }
}
return undefined;
}

findElement([1, 2, 3, 4], num => 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/96.0.4664.93 Safari/537.36

Challenge: Finders Keepers

Link to the challenge:

1 Like

If you pass the return inside else the loop will stop in the first run. That’s why the test fail.

Also notice the code could be shorten quite a bit, as not adding a return value is the same than returning undefined. Hence, the whole else statement plus the return can be removed.

Ok.

Lets consider the example in your question.

findElement([1, 2, 3, 4], num => num % 2 === 0);

now the findElement function will start to execute.

initially you are setting the variable num

so, num will be 0

num → 0

then iteration on the given array will begin.

num will set as the first value in the array, which is 1

num → 1

and runs the conditionals

if(1 => 1 % 2 === 0)

Obviously it will return false

so the else will run

And here is the real problem. If you put return undefined in the else block the execution of the function findElement will immediately stop and return undefined. As you know we had only checked the first element in the array ,but the function exited without checking all the elements. and you are not supposed to do that. We have to check all the elements in the array then only we should exit the function. So strictly no return inside the for loop. After the loop is completed meaning we checked all elements in array then return undefined. Thats what the return undefined at the end is doing.

I tried my best to explain the problem, so the answer is lil lengthy… Hope you understood the concept

1 Like

By returning undefined within else I am stopping the loop and not allowing it to iterate through all the values. Outside the loop, if no values have been successfully executed by the function, undefined could be used.

However, it is also unnecessary because the engine will return undefined if the function is unable to execute successfully. As @anon22924398 said, it is unnecessary.

Am I accurate?

Yep you are.

function findElement(arr, func) {
let num = 0;
for(let i = 0; i < arr.length; i++){
  num = arr[i];
  if(func(num)){
    return num;
  } 
}
1 Like

no, in that case it errors out and stop.

It’s just that the default return value is undefined

1 Like

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