2014-03-14 14 views
0

剛剛學習JavaScript和有困難時期與此代碼想使它重複5次不同的隨機數學問題,但只有當答對繼續下一個問題。有人能幫助我指出正確的方向嗎?如何正確巢while和for循環在JavaScript

// var initialization 
var num1 = Math.floor(Math.random()*100)+1; 
var num2 = Math.floor(Math.random()*100)+1; 
var correct = num1 + num2; 
var guess = 0; 
var count = 1; 
var msg = " "; 
//var debug = 5; 

// loop basic math process 

while(count <= 1){ 

guess = eval(prompt("What is "+num1+" + "+num2+" = __ ?")); 

    if(guess != correct){ 
     msg = alert("Sorry please try again"); 

    }else{ 
     msg = alert("By George I think you got it!"); 

     for(i=0;i<=5;i+=1){ 
      alert(debug); 
      var num1 = Math.floor(Math.random()*100)+1; 
      var num2 = Math.floor(Math.random()*100)+1; 
      var correct = num1 + num2; 

      guess = eval(prompt("What is "+num1+" + "+num2+" = __ ?")); 

     if(guess != correct){ 
      alert("Sorry please try again"); 

     }else{ 
      alert("Great your on a roll!"); 

     }}} count++; 
} 
+0

認沽'計數++'你'else'條件內警報後'(」偉大的你在一卷!)。 – PHPglue

+0

感謝您的快速響應。 – jusBlaze13

+0

使用console.log()輸出以進行調試;它遠遠優於alert(可以在一個好的瀏覽器中直接顯示項目,元素等,而不僅僅是可怕的Object [對象]。對於這個問題,使用錯誤控制檯也可以看到錯誤消息!好的 - 只要我修正了在Chrome中拋出錯誤的'alert(debug)' –

回答

0

如果你想繼續問,直到他們得到的問題的權利,那麼你將需要兩個循環。一個來計算5個問題。還有一個內部的問題,直到它是正確的問題繼續問我認爲你有你的循環倒轉。我反而做類似如下:

for(i=0;i<=5;i+=1){ 
    alert(debug); 
    var num1 = Math.floor(Math.random()*100)+1; 
    var num2 = Math.floor(Math.random()*100)+1; 
    var correct = num1 + num2; 
    var guess = -1; 

    while (guess != correct) {  
     guess = eval(prompt("What is "+num1+" + "+num2+" = __ ?")); 
     if(guess != correct){ 
      msg = alert("Sorry please try again"); 
     }else{ 
      msg = alert("By George I think you got it!"); 
     } 
    } 
} 

如果你想知道,如果他們得到了連續幾個有權改變你必須檢測,如果他們在連勝消息:

var streak = 0; 


    while (guess != correct) {  
     guess = eval(prompt("What is "+num1+" + "+num2+" = __ ?")); 
     if(guess != correct){ 
      msg = alert("Sorry please try again"); 
      streak = 0; 
     }else{ 
      streak++; 
      if (streak < 2) { 
       msg = alert("By George I think you got it!"); 
      } else { 
       msg = alert("Keep up the good work!"); 
      } 
     } 
    } 
+0

感謝這個工作出色了! – jusBlaze13

0

從你的代碼,我想你不需要外循環。 如果你確實需要while循環來控制總回合循環,你最好換爲循環代碼整個在一個單獨的功能。

0

就像一個提示 - 如果你的編輯器沒有重新格式化/重新縮進源代碼的能力,jsbeautifer.org做的很好。

var num1; 
var num2; 
var guess; 
var correct; 
var msg; 

// Bonus 
// It looked like you were trying to give different responses so it wasn't 
// quite as boring 

var successMessages = [ 
    "Way to go!", 
    "Alright!", 
    "Cheers!", 
    "Tally ho!" 
    "Well done, old boy" 
]; 

var questionsLeft = 5; 

// loop basic math process 
while (questionsLeft !== 0) { 

    num1 = Math.floor(Math.random() * 100) + 1; 
    num2 = Math.floor(Math.random() * 100) + 1; 
    correct = num1 + num2; 

    guess = prompt("What is " + num1 + " + " + num2 + " = __ ? "); 
    guess = parseInt(guess, 10); // Convert to a number 

    if (guess !== correct) { 
    msg = alert("Sorry please try again "); 
    } else { 
    msg = successMessages[questionsLeft]; 
    questionsLeft--; 
    } 
} 

或者,如果你想使他們重複問題:

var num1; 
var num2; 
var guess; 
var correct; 
var msg; 
var isRight; 

// Bonus 
// It looked like you were trying to give different responses so it wasn't 
// quite as boring 

var successMessages = [ 
    "Way to go!", 
    "Alright!", 
    "Cheers!", 
    "Tally ho!" 
    "Well done, old boy" 
]; 

var questionsLeft = 5; 

// loop basic math process 
while (questionsLeft !== 0) { 

    num1 = Math.floor(Math.random() * 100) + 1; 
    num2 = Math.floor(Math.random() * 100) + 1; 
    correct = num1 + num2; 

    // They haven't answered yet, so they can't be right. 
    isRight = false; 

    while (!isRight) { 
    guess = prompt("What is " + num1 + " + " + num2 + " = __ ? "); 
    guess = parseInt(guess, 10); // Convert to a number 

    isRight = guess !== correct 

    if (isRight) { 
     msg = alert("Sorry please try again "); 
    } else { 
     msg = successMessages[questionsLeft]; 
     questionsLeft--; 
    } 
    } 
} 
+0

耶還是自己熟悉與sublimetext編輯器。謝謝你的建議,我會嘗試jsbeau tifer.org下次我發佈在這裏。 – jusBlaze13

0

試試這個:

var num1 = [], num2 = [], correct = [], count = 0, guess; 
for(var i=0; i<5; i++){ 
    num1[i] = Math.floor(Math.random()*101); 
    num2[i] = Math.floor(Math.random()*101); 
    correct[i] = num1[i]+num2[i]; 
} 
while(count < 5){ 
    quess = prompt('What is '+num1[count]+'+'+num2[count]+' ?'); 
    if(+quess === correct[count]){ 
    alert('You are Correct!'); 
    if(++count === 5)alert('You Have Completed All of the Answers Correctly!!!'); 
    } 
    else{ 
    alert('Please Try Again.') 
    } 
} 
+0

感謝您的快速響應 – jusBlaze13