如何在javascript中正确嵌套while和for循环

时间:2014-03-14 01:05:54

标签: javascript if-statement for-loop while-loop

只是学习javascript并且很难使用这段代码,想让它重复5次不同的随机数学问题,但只有正确回答才能继续下一个问题。有人可以帮助我指出正确的方向吗?

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

4 个答案:

答案 0 :(得分:0)

根据您的编码,我认为您不需要外部 while 循环。 如果你确实需要while循环来控制 for 循环的总轮数,你最好将整个 for 循环代码包装在一个单独的函数中。

答案 1 :(得分: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!");
            }
        }
    }

答案 2 :(得分: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--;
    }
  }
}

答案 3 :(得分: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.')
  }
}