Recursion and conditional confusion
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 |
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 '18 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 '18 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 '18 at 7:40
add a comment |
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 '18 at 12:39
George Jempty
6,9711161131
6,9711161131
asked Nov 11 '18 at 10:12
MarkamillionMarkamillion
61
61
The concept used is not closure, but recursion.
– trincot
Nov 11 '18 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 '18 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 '18 at 7:40
add a comment |
The concept used is not closure, but recursion.
– trincot
Nov 11 '18 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 '18 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 '18 at 7:40
The concept used is not closure, but recursion.
– trincot
Nov 11 '18 at 10:14
The concept used is not closure, but recursion.
– trincot
Nov 11 '18 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 '18 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 '18 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 '18 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 '18 at 7:40
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 10:30
answered Nov 11 '18 at 10:24
trincottrincot
121k1586118
121k1586118
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.
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 '18 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 '18 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 '18 at 7:40