How to exit a loop inside a functionin Javascript
I am trying to exit a function call when a value is found, and I can not wrap my head around this.
Using the debugger I've noticed that the return found;
line makes execution jump to the end of the function, OK... but after that another iteration on the for
loop is started.
I've tried using break too.
My code is below.
Edited
I've checked the logic on the debugger, adding a breakpoint on found = true
and it works fine, so the only thing left is that it should exit properly after that...
// Tree traversal until answer found via answer.id
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
found = true;
return found; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
};
javascript function for-loop return break
add a comment |
I am trying to exit a function call when a value is found, and I can not wrap my head around this.
Using the debugger I've noticed that the return found;
line makes execution jump to the end of the function, OK... but after that another iteration on the for
loop is started.
I've tried using break too.
My code is below.
Edited
I've checked the logic on the debugger, adding a breakpoint on found = true
and it works fine, so the only thing left is that it should exit properly after that...
// Tree traversal until answer found via answer.id
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
found = true;
return found; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
};
javascript function for-loop return break
return
should work.
– Nina Scholz
Nov 28 '18 at 12:05
add a comment |
I am trying to exit a function call when a value is found, and I can not wrap my head around this.
Using the debugger I've noticed that the return found;
line makes execution jump to the end of the function, OK... but after that another iteration on the for
loop is started.
I've tried using break too.
My code is below.
Edited
I've checked the logic on the debugger, adding a breakpoint on found = true
and it works fine, so the only thing left is that it should exit properly after that...
// Tree traversal until answer found via answer.id
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
found = true;
return found; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
};
javascript function for-loop return break
I am trying to exit a function call when a value is found, and I can not wrap my head around this.
Using the debugger I've noticed that the return found;
line makes execution jump to the end of the function, OK... but after that another iteration on the for
loop is started.
I've tried using break too.
My code is below.
Edited
I've checked the logic on the debugger, adding a breakpoint on found = true
and it works fine, so the only thing left is that it should exit properly after that...
// Tree traversal until answer found via answer.id
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
found = true;
return found; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
};
javascript function for-loop return break
javascript function for-loop return break
edited Nov 28 '18 at 13:32
P. Navarro
asked Nov 28 '18 at 11:59
P. NavarroP. Navarro
156
156
return
should work.
– Nina Scholz
Nov 28 '18 at 12:05
add a comment |
return
should work.
– Nina Scholz
Nov 28 '18 at 12:05
return
should work.– Nina Scholz
Nov 28 '18 at 12:05
return
should work.– Nina Scholz
Nov 28 '18 at 12:05
add a comment |
2 Answers
2
active
oldest
votes
You should use break
inside the loop and return statement at the end of the function. Please updated code
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
var found = true;
break; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
return found;
};
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
Try to use break instead of return inside the for loop
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
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%2f53518974%2fhow-to-exit-a-loop-inside-a-functionin-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use break
inside the loop and return statement at the end of the function. Please updated code
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
var found = true;
break; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
return found;
};
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
You should use break
inside the loop and return statement at the end of the function. Please updated code
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
var found = true;
break; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
return found;
};
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
You should use break
inside the loop and return statement at the end of the function. Please updated code
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
var found = true;
break; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
return found;
};
You should use break
inside the loop and return statement at the end of the function. Please updated code
function locateAnswer(wholeTree, answerID) {
var found = false;
if (wholeTree.answers) { // checks if next_queston is populated first
for (var i = 0; i < wholeTree.answers.length; ++i) {
if (wholeTree.answers[i].id == answerID) {
console.log("found!");
var found = true;
break; // TRIED break; TOO
} else {
for (var j = 0; j < $scope.breadcrumbs.length; ++j) {
if ($scope.breadcrumbs[j].question == wholeTree.question) {
// if already exist, we pop elements until it does not exist anymore
while ($scope.breadcrumbs[j]) {
$scope.breadcrumbs.pop();
}
}
}
// we push the new breadcrumb
$scope.breadcrumbs.push({ "question": wholeTree.question, "answer": wholeTree.answers[i].answer });
locateAnswer(wholeTree.answers[i].next_question, answerID);
}
}
}
// ALSO TRIED return HERE AFTER break
return found;
};
answered Nov 28 '18 at 12:01
Gufran HasanGufran Hasan
3,63041628
3,63041628
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
Try to use break instead of return inside the for loop
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
Try to use break instead of return inside the for loop
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
Try to use break instead of return inside the for loop
Try to use break instead of return inside the for loop
answered Nov 28 '18 at 12:01
HaYa Abu Al HalawaHaYa Abu Al Halawa
75
75
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
add a comment |
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
– P. Navarro
Nov 28 '18 at 13:26
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%2f53518974%2fhow-to-exit-a-loop-inside-a-functionin-javascript%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
return
should work.– Nina Scholz
Nov 28 '18 at 12:05