剛剛學習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++;
}
認沽'計數++'你'else'條件內警報後'(」偉大的你在一卷!)。 – PHPglue
感謝您的快速響應。 – jusBlaze13
使用console.log()輸出以進行調試;它遠遠優於alert(可以在一個好的瀏覽器中直接顯示項目,元素等,而不僅僅是可怕的Object [對象]。對於這個問題,使用錯誤控制檯也可以看到錯誤消息!好的 - 只要我修正了在Chrome中拋出錯誤的'alert(debug)' –