2012-10-30 63 views
-1

我現在試了一下,並得到它的完美。我試圖簡化這個for循環我創建並使它實際上工作,沒有任何數組,只有最基本的基本JavaScript。如何簡化Javascript中的簡單循環?

for (var x=0;x<=1;x++) { 
    if (secondInput == luckyNumber || secondInput == luckyNumber2 || secondInput == luckyNumber3) { 
     if (thirdInput == luckyNumber || thirdInput == luckyNumber2 || thirdInput == luckyNumber3) { 
      if (firstInput == luckyNumber || firstInput == luckyNumber2 || firstInput == luckyNumber3) { 
       while (firstInput !== secondInput){ 
        while(firstInput !== thirdInput){while(secondInput !== thirdInput) { 
         alert('Congratulations! You got all 3 numbers correct. You\'ve won £1,000!'); 
        } 
       } 
      } 
     } 
    } 
} 

此代碼是否有意義或我做錯了什麼?我覺得我甚至可以離開這個循環,但這是我認爲這是正確的唯一方式。

+0

你如何從內部'while()'循環中斷? – SparKot

回答

1

編寫一個接受輸入的函數,將它與幸運數字進行比較,並返回一個帶結果的布爾值。

if子句中調用該函數。

我不太明白你想用while循環做什麼。

1

你可以嘗試使用這種想法,以幫助:

[1, 3, 2].sort() 

(存儲您的問題和答案在數組和排序都再對比當然,檢查平等JavaScript數組是一個有趣的新項目:) )

0

你在這裏。你說你想簡化它。

for (var x = 0; 1 >= x; x++) { 
if (!(secondInput != luckyNumber && secondInput != luckyNumber2 && secondInput != luckyNumber3 || thirdInput != luckyNumber && thirdInput != luckyNumber2 && thirdInput != luckyNumber3 || firstInput != luckyNumber && firstInput != luckyNumber2 && firstInput != luckyNumber3)) { 
    while (firstInput !== secondInput) { 
     while (firstInput !== thirdInput) { 
      while (secondInput !== thirdInput) { 
       alert("Congratulations! You got all 3 numbers correct. You\'ve won £1,000!"); 
      } 
     } 
    } 
} 
}