Skip to content Skip to sidebar Skip to footer

Understanding Recursive Loop That Returns An Inverted Count

inner conditional: 5 inner conditional: 4 inner conditional: 3 inner conditional: 2 inner conditional: 1 0 The function that created the above output is below and I perfectly unde

Solution 1:

The problem is, you call the function again and the function ends with a counter < 1, but it has not end the actual function itself and make the output with the counter later.

The same level if indentation shows the same function.

inner conditional: 5inner conditional: 4inner conditional: 3inner conditional: 2inner conditional: 1012345

Solution 2:

From inside the first instance of the loop function you are calling the loop function with a return, that prevents rest of the statements after "return" to execute. However, in the second instance loop function is called without the return, hence the console.log outside the if condition is also executed and you see both the console.log outputs.

Solution 3:

The answer that I was looking for is that the 1-5 count is being pulled off the top of the stack.

I found a great youtube video that explains this here with visualizations:

https://www.youtube.com/watch?v=s8JpA5MjYac

Post a Comment for "Understanding Recursive Loop That Returns An Inverted Count"