How to exit a loop inside a functionin Javascript












0















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
};









share|improve this question

























  • return should work.

    – Nina Scholz
    Nov 28 '18 at 12:05


















0















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
};









share|improve this question

























  • return should work.

    – Nina Scholz
    Nov 28 '18 at 12:05
















0












0








0








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
};









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





















  • 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














2 Answers
2






active

oldest

votes


















1














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;
};





share|improve this answer
























  • 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



















0














Try to use break instead of return inside the for loop






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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;
};





share|improve this answer
























  • 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
















1














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;
};





share|improve this answer
























  • 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














1












1








1







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;
};





share|improve this answer













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;
};






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













0














Try to use break instead of return inside the for loop






share|improve this answer
























  • 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
















0














Try to use break instead of return inside the for loop






share|improve this answer
























  • 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














0












0








0







Try to use break instead of return inside the for loop






share|improve this answer













Try to use break instead of return inside the for loop







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks