简体   繁体   English

如何在JavaScript中同时和for循环嵌套

[英]How do I properly nest while and for loops in javascript

Just learning javascript and having a hard time with this code would like to make it repeat 5 times different random math questions but only continue to the next question if answered correctly. 只是学习javascript并用这段代码很难,它想让它重复5次不同的随机数学问题,但是只有正确回答后,才继续下一个问题。 can somebody help point me in the right direction? 有人可以帮我指出正确的方向吗?

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

From your coding, I suppose you don't need the outer while loop. 根据您的编码,我想您不需要外部的while循环。 And if you do need while loop to control the total rounds of for loop, you should better wrap the whole for loop code in an individual function. 而且,如果确实需要while循环来控制for循环的总次数,则最好将整个for循环代码包装在单个函数中。

If you want to keep asking until they get the question right, then you are going to need two loops. 如果您要一直问直到他们解决问题,那么您将需要两个循环。 One to count the 5 questions. 一算5个问题。 And one inside of the that to keep asking the question until it is correct I think you have your loops kind of inverted. 还有一个问题要不断提出,直到正确为止,我认为您的循环有点颠倒了。 I would instead do something like the following: 我将改为执行以下操作:

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!");
        }
    }
}

If you wanted to know if they got several right in a row to change the message you would have to detect if they were on a streak: 如果您想知道他们是否连续有几个权利来更改消息,则必须检测它们是否处于连胜状态:

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!");
            }
        }
    }

Just as a hint -- if your editor doesn't have the ability to reformat/re-indent source code, jsbeautifer.org does a wonderful job. 只是提示-如果您的编辑器不具备重新格式化/重新缩进源代码的功能,那么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--;
  }
}

Or, if you want to make them repeat the question: 或者,如果您想让他们重复这个问题:

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

Try this: 尝试这个:

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.')
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM