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
    };
1
  • return should work. Commented Nov 28, 2018 at 12:05

2 Answers 2

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;
    };
Sign up to request clarification or add additional context in comments.

1 Comment

Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?
0

Try to use break instead of return inside the for loop

1 Comment

Tried this, does not work either... Is it something related with the recursion? Am I calling it in the wrong place?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.