1

There is a piece of recursive function that I was working on and it seems to be returning the wrong value than what is expected. I was able to replicate it and simplify the function into:

function foo(i, target){    
    if(i < target){
        i++;
        foo(i, target);
    }

    return i;   
}
console.log(foo(0,5));

Basically, given the function above, I expect the return value to be 5. However, it seems to return 1. When I did some tracing, I notice that "return i" is being called several times and each time it gets decremented with 1? What's the reason for this behaviour and what can I do to fix it?

3 Answers 3

5

You need also to return form the if part in your code.

function foo(i, target){    
    if(i < target){
        i++;
        return foo(i, target);
    }
   
    console.log(i);
    return i;   
}

console.log(foo(0,5));

Why your code returns 1?

Because it only calls the foo every time while i < target and after it you get all values returned from the nested calls in order 5, 4, 3, 2, 1 and the last one is returned from the first function call is printed. You can check this by putting simple console.log before the return i and compare to the above result.

function foo(i, target){    
    if(i < target){
        i++;
        foo(i, target);
    }
   
    console.log(i);
    return i;   
}

console.log(foo(0,5));

To visualize the returned values you can see

 console.log()                               console.log()
 |   Call with 1                             -- return Call with 1
 |   |   Call with 2                            -- return Call with 2
 |   |   |   Call with 3                           -- return Call with 3
 |   |   |   |   Call with 4                          -- return Call with 4
 |   |   |   |   |   Call with 5        VS               -- return Call with 5  
 |   |   |   |   |   return 5
 |   |   |   |   return 4
 |   |   |   return 3
 |   |   return 2
 |-- return 1
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks you for you explanation on why it returned 1. It gave me an idea and ran the trace again. There I noticed the call stack with several foo calls and as I step over "return i" , each call gets popped.
See also the visualization I made, it can help you
How do you do the trace with Google Dev Tools?
@Soolie Press F12 and go to Sources tab
yup, there is a "Call Stack" section
2

You are not returning the value of the recursive call

function foo(i, target){    
    if(i < target){
        i++;
        return foo(i, target);
    }

    return i;   
}
console.log(foo(0,5));

Comments

2

You have to give return to the recursive function call as well:

return foo(i, target);

function foo(i, target) {
  if (i < target) {
    i++;
    // Change here...
    return foo(i, target);
  }
  return i;
}
console.log(foo(0, 5));

1 Comment

apologies @Soolie but I have to give it to Suren Srapyan for helping me understand why it was returning 1. cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.