Recursion and conditional confusion
up vote
0
down vote
favorite
I have been looking at this code and scratching my head at it for too long and was hoping to gain insight into how exactly the value 33 is the end result.
I know that this code goes through the conditionals to the else and decrements the value of b until it reaches the else if conditional where the b ==1 condition is met.
What I don't understand, is how within that else if, the value of b is incremented and from then on, so too does the value of 'a' until the final value of 33 is reached.
The other strange feature of this code is that after the else if has made those changes, only the else runs and the other conditionals are no longer checked.
For someone relatively new to recursion, this behavior is truly weird.
function mlt(a, b)
debugger;
if(!(a && b))
return 0;
else if (b == 1)
return a;
else
return (a + mlt(a, b - 1));
console.log(mlt(3, 11));
Copying this in the browser console and running through the loops will give insight into what I am on about.
javascript recursion conditional
add a comment |
up vote
0
down vote
favorite
I have been looking at this code and scratching my head at it for too long and was hoping to gain insight into how exactly the value 33 is the end result.
I know that this code goes through the conditionals to the else and decrements the value of b until it reaches the else if conditional where the b ==1 condition is met.
What I don't understand, is how within that else if, the value of b is incremented and from then on, so too does the value of 'a' until the final value of 33 is reached.
The other strange feature of this code is that after the else if has made those changes, only the else runs and the other conditionals are no longer checked.
For someone relatively new to recursion, this behavior is truly weird.
function mlt(a, b)
debugger;
if(!(a && b))
return 0;
else if (b == 1)
return a;
else
return (a + mlt(a, b - 1));
console.log(mlt(3, 11));
Copying this in the browser console and running through the loops will give insight into what I am on about.
javascript recursion conditional
The concept used is not closure, but recursion.
– trincot
Nov 11 at 10:14
This has nothing to do with closures - there is no closure going on in this code. It's just a straightforward recursive function. I'd be glad to help you in your understanding but I'm not sure where your confusion lies.
– Robin Zigmond
Nov 11 at 10:15
Agreed this has nothing to do with closures. My confusion is how and in what sequence does the mlt function go from a = 3 and b = 1 (after b is decremented to 1) to reaching the final result of 33.
– Markamillion
Nov 12 at 7:40
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have been looking at this code and scratching my head at it for too long and was hoping to gain insight into how exactly the value 33 is the end result.
I know that this code goes through the conditionals to the else and decrements the value of b until it reaches the else if conditional where the b ==1 condition is met.
What I don't understand, is how within that else if, the value of b is incremented and from then on, so too does the value of 'a' until the final value of 33 is reached.
The other strange feature of this code is that after the else if has made those changes, only the else runs and the other conditionals are no longer checked.
For someone relatively new to recursion, this behavior is truly weird.
function mlt(a, b)
debugger;
if(!(a && b))
return 0;
else if (b == 1)
return a;
else
return (a + mlt(a, b - 1));
console.log(mlt(3, 11));
Copying this in the browser console and running through the loops will give insight into what I am on about.
javascript recursion conditional
I have been looking at this code and scratching my head at it for too long and was hoping to gain insight into how exactly the value 33 is the end result.
I know that this code goes through the conditionals to the else and decrements the value of b until it reaches the else if conditional where the b ==1 condition is met.
What I don't understand, is how within that else if, the value of b is incremented and from then on, so too does the value of 'a' until the final value of 33 is reached.
The other strange feature of this code is that after the else if has made those changes, only the else runs and the other conditionals are no longer checked.
For someone relatively new to recursion, this behavior is truly weird.
function mlt(a, b)
debugger;
if(!(a && b))
return 0;
else if (b == 1)
return a;
else
return (a + mlt(a, b - 1));
console.log(mlt(3, 11));
Copying this in the browser console and running through the loops will give insight into what I am on about.
javascript recursion conditional
javascript recursion conditional
edited Nov 11 at 12:39
George Jempty
6,9371161129
6,9371161129
asked Nov 11 at 10:12
Markamillion
61
61
The concept used is not closure, but recursion.
– trincot
Nov 11 at 10:14
This has nothing to do with closures - there is no closure going on in this code. It's just a straightforward recursive function. I'd be glad to help you in your understanding but I'm not sure where your confusion lies.
– Robin Zigmond
Nov 11 at 10:15
Agreed this has nothing to do with closures. My confusion is how and in what sequence does the mlt function go from a = 3 and b = 1 (after b is decremented to 1) to reaching the final result of 33.
– Markamillion
Nov 12 at 7:40
add a comment |
The concept used is not closure, but recursion.
– trincot
Nov 11 at 10:14
This has nothing to do with closures - there is no closure going on in this code. It's just a straightforward recursive function. I'd be glad to help you in your understanding but I'm not sure where your confusion lies.
– Robin Zigmond
Nov 11 at 10:15
Agreed this has nothing to do with closures. My confusion is how and in what sequence does the mlt function go from a = 3 and b = 1 (after b is decremented to 1) to reaching the final result of 33.
– Markamillion
Nov 12 at 7:40
The concept used is not closure, but recursion.
– trincot
Nov 11 at 10:14
The concept used is not closure, but recursion.
– trincot
Nov 11 at 10:14
This has nothing to do with closures - there is no closure going on in this code. It's just a straightforward recursive function. I'd be glad to help you in your understanding but I'm not sure where your confusion lies.
– Robin Zigmond
Nov 11 at 10:15
This has nothing to do with closures - there is no closure going on in this code. It's just a straightforward recursive function. I'd be glad to help you in your understanding but I'm not sure where your confusion lies.
– Robin Zigmond
Nov 11 at 10:15
Agreed this has nothing to do with closures. My confusion is how and in what sequence does the mlt function go from a = 3 and b = 1 (after b is decremented to 1) to reaching the final result of 33.
– Markamillion
Nov 12 at 7:40
Agreed this has nothing to do with closures. My confusion is how and in what sequence does the mlt function go from a = 3 and b = 1 (after b is decremented to 1) to reaching the final result of 33.
– Markamillion
Nov 12 at 7:40
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
This is not an example of closure, but of recursion.
In order to understand how recursion can bring the correct result, it can help to assume for a moment that the recursive call returns the correct result and see if that makes the overall return value correct as well.
Let's take for example this main call:
mlt(20, 4)
The execution will go to the else
part and perform the recursive call:
return (a + mlt(a, b - 1));
As you can derive, the recursive call comes down to mlt(20, 3)
. Now let's just for a moment assume that this recursive call returns the correct result, i.e. 60. See what happens in the above expression:
return (20 + 60);
This is 80, which indeed is the correct result for our original call mlt(20, 4)
. So we can get a feel of how this function will return the correct product for any given b
if we can assume the function does it right for b-1
also.
What happens when b = 1
? Then the function returns a
. We can also see that this is correct. So with the previous conclusion we now can be sure that if b >= 1
the result will be correct.
Note that the function will also do it right for b = 0
as then the first if
kicks in. Also note that this function does not cope well with negative values of b
: in that case the function will keep recurring with b-1
and will eventually bump into a stack capacity error.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This is not an example of closure, but of recursion.
In order to understand how recursion can bring the correct result, it can help to assume for a moment that the recursive call returns the correct result and see if that makes the overall return value correct as well.
Let's take for example this main call:
mlt(20, 4)
The execution will go to the else
part and perform the recursive call:
return (a + mlt(a, b - 1));
As you can derive, the recursive call comes down to mlt(20, 3)
. Now let's just for a moment assume that this recursive call returns the correct result, i.e. 60. See what happens in the above expression:
return (20 + 60);
This is 80, which indeed is the correct result for our original call mlt(20, 4)
. So we can get a feel of how this function will return the correct product for any given b
if we can assume the function does it right for b-1
also.
What happens when b = 1
? Then the function returns a
. We can also see that this is correct. So with the previous conclusion we now can be sure that if b >= 1
the result will be correct.
Note that the function will also do it right for b = 0
as then the first if
kicks in. Also note that this function does not cope well with negative values of b
: in that case the function will keep recurring with b-1
and will eventually bump into a stack capacity error.
add a comment |
up vote
1
down vote
This is not an example of closure, but of recursion.
In order to understand how recursion can bring the correct result, it can help to assume for a moment that the recursive call returns the correct result and see if that makes the overall return value correct as well.
Let's take for example this main call:
mlt(20, 4)
The execution will go to the else
part and perform the recursive call:
return (a + mlt(a, b - 1));
As you can derive, the recursive call comes down to mlt(20, 3)
. Now let's just for a moment assume that this recursive call returns the correct result, i.e. 60. See what happens in the above expression:
return (20 + 60);
This is 80, which indeed is the correct result for our original call mlt(20, 4)
. So we can get a feel of how this function will return the correct product for any given b
if we can assume the function does it right for b-1
also.
What happens when b = 1
? Then the function returns a
. We can also see that this is correct. So with the previous conclusion we now can be sure that if b >= 1
the result will be correct.
Note that the function will also do it right for b = 0
as then the first if
kicks in. Also note that this function does not cope well with negative values of b
: in that case the function will keep recurring with b-1
and will eventually bump into a stack capacity error.
add a comment |
up vote
1
down vote
up vote
1
down vote
This is not an example of closure, but of recursion.
In order to understand how recursion can bring the correct result, it can help to assume for a moment that the recursive call returns the correct result and see if that makes the overall return value correct as well.
Let's take for example this main call:
mlt(20, 4)
The execution will go to the else
part and perform the recursive call:
return (a + mlt(a, b - 1));
As you can derive, the recursive call comes down to mlt(20, 3)
. Now let's just for a moment assume that this recursive call returns the correct result, i.e. 60. See what happens in the above expression:
return (20 + 60);
This is 80, which indeed is the correct result for our original call mlt(20, 4)
. So we can get a feel of how this function will return the correct product for any given b
if we can assume the function does it right for b-1
also.
What happens when b = 1
? Then the function returns a
. We can also see that this is correct. So with the previous conclusion we now can be sure that if b >= 1
the result will be correct.
Note that the function will also do it right for b = 0
as then the first if
kicks in. Also note that this function does not cope well with negative values of b
: in that case the function will keep recurring with b-1
and will eventually bump into a stack capacity error.
This is not an example of closure, but of recursion.
In order to understand how recursion can bring the correct result, it can help to assume for a moment that the recursive call returns the correct result and see if that makes the overall return value correct as well.
Let's take for example this main call:
mlt(20, 4)
The execution will go to the else
part and perform the recursive call:
return (a + mlt(a, b - 1));
As you can derive, the recursive call comes down to mlt(20, 3)
. Now let's just for a moment assume that this recursive call returns the correct result, i.e. 60. See what happens in the above expression:
return (20 + 60);
This is 80, which indeed is the correct result for our original call mlt(20, 4)
. So we can get a feel of how this function will return the correct product for any given b
if we can assume the function does it right for b-1
also.
What happens when b = 1
? Then the function returns a
. We can also see that this is correct. So with the previous conclusion we now can be sure that if b >= 1
the result will be correct.
Note that the function will also do it right for b = 0
as then the first if
kicks in. Also note that this function does not cope well with negative values of b
: in that case the function will keep recurring with b-1
and will eventually bump into a stack capacity error.
edited Nov 11 at 10:30
answered Nov 11 at 10:24
trincot
114k1477109
114k1477109
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247689%2frecursion-and-conditional-confusion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The concept used is not closure, but recursion.
– trincot
Nov 11 at 10:14
This has nothing to do with closures - there is no closure going on in this code. It's just a straightforward recursive function. I'd be glad to help you in your understanding but I'm not sure where your confusion lies.
– Robin Zigmond
Nov 11 at 10:15
Agreed this has nothing to do with closures. My confusion is how and in what sequence does the mlt function go from a = 3 and b = 1 (after b is decremented to 1) to reaching the final result of 33.
– Markamillion
Nov 12 at 7:40