为什么答案的顺序不能按预期工作?

时间:2014-11-04 19:51:07

标签: javascript if-statement

我向用户提出五个问题,需要用户回答。计算正确的答案,然后在最后对用户进行评分。

我希望每个问题的评分在每个问题之后立即出现,而不是在问到所有问题之后。就像现在一样,它会询问5个问题中的2个问题,然后向用户提供回复,但只有在问到所有问题之后。顺序是:(问,问,答案,答案),但我需要它(问,答案,问,答案),但我无法弄清楚为什么不这样做。

// Counters
var numberOfQuestions = 5;
var correctAnswers = 0;

// The five questions
var raining = prompt("Is it raining today?");
var married = prompt("Am I married?");
    /* Remaining questions
    var day = prompt("What day is it?");
    var threes = prompt("What does 3 + 3 + 3 equal?");
    var number = prompt("What number am I thinking of?"); 
    */

// First question - Is it raining
if (raining.toUpperCase() === "NO") {
  correctAnswers += 1;
  alert("Correct. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);  
} else {
    alert("Don't you wish it was. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);
  }

// Second question - Am I married
if (married.toUpperCase() === "YES") {
  correctAnswers += 1;
  alert("Correct! I am married. \nYou now have " + correctAnswers + " correct answers out of " + numberOfQuestions);
} else {
  alert("Incorrect, I am married. \n You still have " + correctAnswers + " correct answers out of " + numberOfQuestions);
}

任何帮助都将非常感激。

4 个答案:

答案 0 :(得分:1)

放置

var married = prompt("Am I married?");

在第二个if之前。

将变量分配给prompt()会立即显示提示 ,而不是当您开始对此变量执行某些操作时。

答案 1 :(得分:1)

很简单地说:

var married = prompt("Am I married?");

后:

// Second question - Am I married

答案 2 :(得分:0)

简单的方法是在提示之间交替if检查。但是,您可能希望使用函数来帮助保持正确:

function ask(question, correct_answer, correct_text, incorrect_text) {

    var answer = prompt(question);
    
    if (answer.toUpperCase() === correct_answer.toUpperCase()) {
        alert(correct_text + "\nYou have " + (++correct_count) + " out of " + question_count);
    } else {
        alert(incorrect_text + "\nYou have " + (correct_count) + " out of " + question_count);
    }

}

function run() {
    correct_count = 0;
    question_count = 2;

    ask("Is it raining?", "YES", "Correct, it is!", "Sorry, but it actually is.");
    ask("Am I married?", "YES", "Correct, I am!", "Sorry, but I've got the ring and everything.");

    resolve();
}

function resolve() {
    alert("You got " + correct_count + " questions right!");
}

run();

答案 3 :(得分:-1)

这应该有效。它基于您的原始代码。enter code here

var raining = prompt("Is it raining today?");

if (raining.toUpperCase() === "NO") {
  correctAnswers += 1;
  alert("Correct. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);  
} else {
    alert("Don't you wish it was. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);
  }


var married = prompt("Am I married?");
    /* Remaining questions
    var day = prompt("What day is it?");
    var threes = prompt("What does 3 + 3 + 3 equal?");
    var number = prompt("What number am I thinking of?"); 
    */



// Second question - Am I married
if (married.toUpperCase() === "YES") {
  correctAnswers += 1;
  alert("Correct! I am married. \nYou now have " + correctAnswers + " correct answers out of " + numberOfQuestions);
} else {
  alert("Incorrect, I am married. \n You still have " + correctAnswers + " correct answers out of " + numberOfQuestions);
}