2
    function even(num) {//console.log(num) => shows 1 & then 0 before terminating.
            if (num === 0) {
                return true;
            } else {
                return !even(num - 1);
            }
        }
    console.log(even(1));
//Why Chrome Console shows num = 1 when .log is being executed.

The Else part of the recursive function even would run till num becomes 0 this is quite clear but the chrome developer console shows num = 1 print when console.log is logging

Is the final value going to be 0 or 1?

Screenshot after the calls are completed:

enter image description here

8
  • 1
    Your question is unclear on what is the problem and what you expect the output. Commented Mar 30, 2018 at 4:50
  • 1
    You have a breakpoint at line two. continue the code Commented Mar 30, 2018 at 4:51
  • @JayHarris it is there because I am trying to show the final value num is 1 when I was expecting it to be 0 Commented Mar 30, 2018 at 5:39
  • @AnkitAgarwal Im trying to determine the final value of num Commented Mar 30, 2018 at 5:40
  • So press play and it will break again. the function is going to run twice. you want to see the break the second time. F8 is continue btw Commented Mar 30, 2018 at 5:41

3 Answers 3

1

The function you provided will recursively call itself until num is 0, and then the results will bubble back up.

So:

  1. even( 1 ) is called - current function: even( 1 )
  2. even( 0 ) is called by even( 1 ) - current function: even( 0 )
  3. even( 0 ) returns true back to even( 1 ) - current function: even( 1 )
  4. even( 1 ) returns !true: false - current function: main, but even( 1 ) was the last function called.

At the end, the last version of even() the code was running in was even( 1 ). The debugger will show num = 1 because that was the last value num had before returning.

To see this in action:

function even(num) {
  console.log( "num is: " + num + " before function" );
  if (num === 0) {
    return true;
  } else {
    var returnValue = !even(num - 1);
    console.log( "num is: " + num + " after recursion" );
    return returnValue;
  }
}
console.log(even(5));

Sign up to request clarification or add additional context in comments.

6 Comments

its correct to say final value of num is 1 & not 0.Is there a name for this behavior?
Sequentially in respect to time: yes, the final value of num is 1. I'm not sure about a specific term for this, but the behaviour arises through recursion's use of the call stack. The diagram in the Call Stack section of this page explains it more visually: medium.freecodecamp.org/…
if the initial param was 2 so would have been the final value?
Yes, tracking num through all the recursive calls would be 2 -> 1 -> 0 -> 1 -> 2
putting console.log(num) at the start of the function body just logs 1 & then 0 before returning false.I assume the final value showing in console(num = 1) is not accessible?
|
1

The response will be false for odd numbers, and true for even. That looks pretty exactly what you need. But this looks overcomplex, why don't do just

function even(num) {
    return n % 2 == 0;
}

1 Comment

This certainly is better but I wanted to know why the final value of num shows 1 in console and not 0
1

Yes chrome showing right when we use that kind of data structure like recursion then here what happen all call going to stack and all operation happening according to stack push and pop operation. So

when you pass 1 to function then basically what is happening the value of num is -

1 -> 0 in stack but when your condition is true now stack is releasing so it will back on original state and the last value is 1 so that why you are getting 1.

Comments

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.