如何保持正确答案的分数并扣除错误答案的分数?

时间:2019-02-18 18:36:27

标签: javascript loops input output do-while

使用do while循环,我试图保持正确回答的问题的分数,并希望扣除错误回答的问题的分数。我敢肯定有办法做到这一点,我想我只需要一点帮助。

var answer1 = "Batman";
var answer = false;
do {
   var guess = prompt("Who is knows as the Caped Crusader?");
    if (guess == answer1) {
      alert("Yes, you got it right! The Caped Crusader is known as " + answer1 + " You get 1 point!");
      answer = true;
    }
  else {
    alert("Sorry, wrong hero. Try again!");
  }
}
while (answer == false);

1 个答案:

答案 0 :(得分:0)

您可以只添加一个变量保持得分。将其加1可加点,减1可减点。

var answer1 = "Batman";
var answer = false;
var score = 0;

do {
  var guess = prompt("Who is knows as the Caped Crusader?");
  if (guess == answer1) {
    alert("Yes, you got it right! The Caped Crusader is known as " + answer1 + " You get 1 point!");
    score++;
    answer = true;
  } else {
    alert("Sorry, wrong hero. Try again!");
    score--;
  }
}
while (answer == false);

alert("Your score is: " + score);