Callum Norris
Callum Norris

Reputation: 51

Issues with breaking a loop

So I'm trying to create a program that iterates through 2D array until it finds a matching ticket; however when the program finds the number within the array it doesn't break:

    var namesArray = ["Reece", "Jack", "Lewis", "Ryan"];

    var raffleArray = [
      [193, 109, 97, 479],
      [489, 046, 387, 830],
      [475, 947, 294, 590],
      [330, 395, 750, 649]
    ];

    //function getRandomNum(){
    //  return Math,floor(Math.random() * (999 - ))
    //}

    //loops through the 2D array to search for 
    for (j = 0; j < 5; j++) {
      for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { //294 is only set for testing
          alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
          break;
        } else {
          alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
      }
    }}

Hope someone can help!

Upvotes: 2

Views: 86

Answers (4)

Abhi
Abhi

Reputation: 9005

Take a boolean variable to find the required element is matched or not. once it is found break inner loop. where you have to assign true to boolean variable. if boolean var is true then break outer loop.

var isFound = false;

   //loops through the 2D array to search for 
    for (j = 0; j < 5; j++) {
      for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { //294 is only set for testing
            found = true;
          alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
          break;
        } else {
          alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
      }
      if(isFound) {
        break;
      }
    }}

Upvotes: 1

Jeremy Jackson
Jeremy Jackson

Reputation: 2257

You'll need to set a flag to stop the outer loop.

var namesArray = ["Reece", "Jack", "Lewis", "Ryan"];

var raffleArray = [
    [193, 109, 97, 479],
    [489, 046, 387, 830],
    [475, 947, 294, 590, 294],
    [330, 395, 750, 649]
];

//function getRandomNum(){
//  return Math,floor(Math.random() * (999 - ))
//}

//loops through the 2D array to search for
var winner = false;
for (j = 0; j < 5; j++) {
  if(!winner) {
    for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { 
            //294 is only set for testing
            winner = true;
            alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
            break;
        } else {
            alert("Ticket: " + raffleArray[j][i] + " did not win")
        }
    }
  }
}

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 17708

You can use a temporary variable to break the outer loop as:

    var found = false;

   //loops through the 2D array to search for 
    for (j = 0; j < 5; j++) {
      for (i = 0; i < raffleArray[j].length; i++) {
        if (raffleArray[j][i] == 294) { //294 is only set for testing
            found = true;
          alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
          break;
        } else {
          alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
      }
      if(found) {
        break;
      }
    }}

Upvotes: 0

DrC
DrC

Reputation: 7698

You need to label the outer loop to break out of both.

  search:for (j = 0; j < 4; j++) {
    for (i = 0; i < raffleArray[j].length; i++) {
      if (raffleArray[j][i] == 294) { //294 is only set for testing
        alert(namesArray[j] + " has won the raffle with ticket number:         " + raffleArray[j][i]);
        break search;
      }
  }}

Upvotes: 4

Related Questions