will javascript “break” stop a recursive function completely or just that instance?
If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:
//take a 2d array and check if it is in _switch
function checkSwitch(arr2d, rates, n = 0, found = ){
if(n===arr2d.length){
inCase(found, rates)
break
}
for(let i in arr2d[n]){
if(deepCheck(_switch, [...found, arr2d[n][i]]))
checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
}
return false
}
I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.
However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.
So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.
CORRECTED CODE AND LESSONS LEARNED
I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
let tf = false
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return false;
} else {
tf = true
}
}
}
return tf;
}
Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.
javascript arrays
add a comment |
If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:
//take a 2d array and check if it is in _switch
function checkSwitch(arr2d, rates, n = 0, found = ){
if(n===arr2d.length){
inCase(found, rates)
break
}
for(let i in arr2d[n]){
if(deepCheck(_switch, [...found, arr2d[n][i]]))
checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
}
return false
}
I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.
However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.
So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.
CORRECTED CODE AND LESSONS LEARNED
I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
let tf = false
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return false;
} else {
tf = true
}
}
}
return tf;
}
Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.
javascript arrays
add a comment |
If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:
//take a 2d array and check if it is in _switch
function checkSwitch(arr2d, rates, n = 0, found = ){
if(n===arr2d.length){
inCase(found, rates)
break
}
for(let i in arr2d[n]){
if(deepCheck(_switch, [...found, arr2d[n][i]]))
checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
}
return false
}
I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.
However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.
So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.
CORRECTED CODE AND LESSONS LEARNED
I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
let tf = false
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return false;
} else {
tf = true
}
}
}
return tf;
}
Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.
javascript arrays
If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:
//take a 2d array and check if it is in _switch
function checkSwitch(arr2d, rates, n = 0, found = ){
if(n===arr2d.length){
inCase(found, rates)
break
}
for(let i in arr2d[n]){
if(deepCheck(_switch, [...found, arr2d[n][i]]))
checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
}
return false
}
I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.
However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.
So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.
CORRECTED CODE AND LESSONS LEARNED
I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
let tf = false
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return false;
} else {
tf = true
}
}
}
return tf;
}
Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.
javascript arrays
javascript arrays
edited Nov 20 '18 at 4:56
asked Nov 20 '18 at 3:46
user10302261
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You could simply use return
instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return true;
}
}
}
return false;
}
Here, returning true
basically means "stop everything" (break out), and returning false
means stop the current iteration normally (as OP explained himself in the comments below).
This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The firstfalse
return value will stop the loop, as it willreturn
back immediately. Refer to theif (!checkSwitch(...))
part.
– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior Thethrow
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.
– Jeto
Nov 20 '18 at 4:23
1
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
|
show 2 more comments
Neither. Loop control statements like break
and continue
are only valid inside a loop (for
, while
, do
, or switch
). The break
statement in your code is not in a loop, so this code will cause a syntax error.
The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
add a comment |
A break
statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch
return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
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%2f53385924%2fwill-javascript-break-stop-a-recursive-function-completely-or-just-that-instan%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could simply use return
instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return true;
}
}
}
return false;
}
Here, returning true
basically means "stop everything" (break out), and returning false
means stop the current iteration normally (as OP explained himself in the comments below).
This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The firstfalse
return value will stop the loop, as it willreturn
back immediately. Refer to theif (!checkSwitch(...))
part.
– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior Thethrow
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.
– Jeto
Nov 20 '18 at 4:23
1
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
|
show 2 more comments
You could simply use return
instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return true;
}
}
}
return false;
}
Here, returning true
basically means "stop everything" (break out), and returning false
means stop the current iteration normally (as OP explained himself in the comments below).
This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The firstfalse
return value will stop the loop, as it willreturn
back immediately. Refer to theif (!checkSwitch(...))
part.
– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior Thethrow
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.
– Jeto
Nov 20 '18 at 4:23
1
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
|
show 2 more comments
You could simply use return
instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return true;
}
}
}
return false;
}
Here, returning true
basically means "stop everything" (break out), and returning false
means stop the current iteration normally (as OP explained himself in the comments below).
This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.
You could simply use return
instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":
function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return true;
}
}
}
return false;
}
Here, returning true
basically means "stop everything" (break out), and returning false
means stop the current iteration normally (as OP explained himself in the comments below).
This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.
edited Nov 20 '18 at 4:38
answered Nov 20 '18 at 4:08
JetoJeto
5,77021119
5,77021119
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The firstfalse
return value will stop the loop, as it willreturn
back immediately. Refer to theif (!checkSwitch(...))
part.
– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior Thethrow
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.
– Jeto
Nov 20 '18 at 4:23
1
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
|
show 2 more comments
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The firstfalse
return value will stop the loop, as it willreturn
back immediately. Refer to theif (!checkSwitch(...))
part.
– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior Thethrow
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.
– Jeto
Nov 20 '18 at 4:23
1
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).
– user10302261
Nov 20 '18 at 4:19
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first
false
return value will stop the loop, as it will return
back immediately. Refer to the if (!checkSwitch(...))
part.– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first
false
return value will stop the loop, as it will return
back immediately. Refer to the if (!checkSwitch(...))
part.– Jeto
Nov 20 '18 at 4:21
@CapitalJusticeWarrior The
throw
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.– Jeto
Nov 20 '18 at 4:23
@CapitalJusticeWarrior The
throw
route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.– Jeto
Nov 20 '18 at 4:23
1
1
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!
– user10302261
Nov 20 '18 at 4:28
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.
– user10302261
Nov 20 '18 at 4:34
|
show 2 more comments
Neither. Loop control statements like break
and continue
are only valid inside a loop (for
, while
, do
, or switch
). The break
statement in your code is not in a loop, so this code will cause a syntax error.
The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
add a comment |
Neither. Loop control statements like break
and continue
are only valid inside a loop (for
, while
, do
, or switch
). The break
statement in your code is not in a loop, so this code will cause a syntax error.
The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
add a comment |
Neither. Loop control statements like break
and continue
are only valid inside a loop (for
, while
, do
, or switch
). The break
statement in your code is not in a loop, so this code will cause a syntax error.
The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.
Neither. Loop control statements like break
and continue
are only valid inside a loop (for
, while
, do
, or switch
). The break
statement in your code is not in a loop, so this code will cause a syntax error.
The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.
edited Nov 20 '18 at 3:52
answered Nov 20 '18 at 3:50
duskwuffduskwuff
148k19177234
148k19177234
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
add a comment |
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
do I do a return then? maybe a continue?
– user10302261
Nov 20 '18 at 3:51
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!
– user10302261
Nov 20 '18 at 3:57
add a comment |
A break
statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch
return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
add a comment |
A break
statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch
return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
add a comment |
A break
statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch
return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).
A break
statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch
return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).
answered Nov 20 '18 at 3:51
Ted HoppTed Hopp
200k41314426
200k41314426
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
add a comment |
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
Ok I will rewrite it with a return and a condition check then. Thank you!
– user10302261
Nov 20 '18 at 3:55
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
nvm I will try/catch an exception when I call it thanks! Way easier on my head!
– user10302261
Nov 20 '18 at 4:07
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%2f53385924%2fwill-javascript-break-stop-a-recursive-function-completely-or-just-that-instan%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