Search code examples
javascriptalgorithmfor-loopcontinue

Continue in nested for loops


I have 3 for-loop inside each other:

      for (var i = 0; i < ns_match.length; i++) {
        for (var j = 0; j < user_interest.length; j++) {
          for (var k = 0; k < ns_match[j].data().user_interest.length; k++) {
            if (user_interest[j] == (ns_match[i].data().user_interest)[k]) {
              ns_score[i] += 5;
            }
          }
        }
      }

I would add a logic such as if (user_interest[j] == (ns_match[i].data().user_interest)[k]) { is true, then augment j.

In another word, I am looking for something such as: if (user_interest[j] == (ns_match[i].data().user_interest)[k]) { if that is true, then break out of of the K loop, but continue one increment in the J loop.


Solution

  • use the break command to break out of the current loop block.

    if you would need to break out of a different loop other than the inner-most one, you would want to look into whats called 'labels'