所以我试图创建一个程序,它遍历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
}
}}
希望有人能帮助!
歇只会打破你内for循环.. – Keith
'为(J = 0;Ĵ<5; J ++)''namesArray'has只有4个成员,所以不宜它是'Ĵ<4' ,甚至更好'j
connexo
你的'j <5'也是错的,..只有4个元素..也许'j
Keith