打破循环的问题

时间:2016-11-14 16:47:11

标签: javascript arrays loops break

所以我试图创建一个迭代2D数组的程序,直到找到匹配的票证;但是当程序找到数组中的数字时,它不会中断:

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

希望有人可以提供帮助!

4 个答案:

答案 0 :(得分:5)

你需要标记外部循环以打破两者。

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

答案 1 :(得分:1)

取一个布尔变量来查找所需元素是否匹配。一旦发现破坏内循环。你必须为布尔变量赋值true。如果boolean var为true则打破外部循环。

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

答案 2 :(得分:0)

您可以使用临时变量将外部循环分解为:

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

答案 3 :(得分:0)

您需要设置一个标志来停止外循环。

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