【问题标题】:The function doesn't give me the correct result该功能没有给我正确的结果
【发布时间】:2020-10-25 07:49:01
【问题描述】:

我正在尝试在 JavaScript 中构建一个功能,将随机问题连同 3 个可能的答案一起发送到提示中。用户给出一个答案,该函数在警报框中显示结果(无论答案是否正确)。正是在那里我遇到了麻烦。因为无论给出的答案是对还是错,它总是会表明答案是不正确的。我已经检查了代码 100 次,但找不到我的错误。有人可以帮助我解释我哪里出错了吗?

此外,如果在不使用 JQuery 的情况下对代码有任何改进,我很想听听!我最近才开始学习 JS,所以欢迎任何意见!

// Build function constructor for the questions with inside: the question, the answers and the correct answer.
function Question(question, [answer1, answer2, answer3], correctAnswer) {
  // Add an instance to Question to count the total amount of questions.
  Question.instances++;
  // Create the blueprint for the questions
  this.theQuestion = question;
  this.theAnswer = [answer1, answer2, answer3];
  this.correctAnswer = correctAnswer;
  // Check if the answer given is correct
  this.checkAnswer = function(givenAnswer) {
    console.log(this.correctAnswer + ' ' + givenAnswer);
    if (this.correctAnswer === givenAnswer) {
      alert('Well done, that answer is correct!');
    } else {
      alert('Sorry, but that is NOT correct!');
    };
  }
}
// Set the total amount of questions to 0
Question.instances = 0;

// Create an empty array to store the questions in
var allQuestions = [];

// Create a couple questions using the Question function constructor
var q0 = new Question('What is my name?', ['Herman', 'Peter', 'Sander'], 0);
var q1 = new Question('How old am I?', [23, 32, 36], 1);
var q2 = new Question('What is the name of my daugther?', ['Julia', 'Sandra', 'Marijke'], 1);
var q3 = new Question('What is the name of my wife?', ['Esther', 'Marijke', 'Vladlena'], 2);

// Push the question to the empty Array
allQuestions.push(q0);
allQuestions.push(q1);
allQuestions.push(q2);
allQuestions.push(q3);

// Create a function that generates a random question into prompt and checks if the answer is correct
function randomQuestion() {
  var randomNr = Math.floor(Math.random() * Question.instances); // Give a random number based on the amount of questions
  var question = allQuestions[randomNr].theQuestion; // Random question based on the number generated
  // Set the possible answers.
  var answer1 = allQuestions[randomNr].theAnswer[0];
  var answer2 = allQuestions[randomNr].theAnswer[1];
  var answer3 = allQuestions[randomNr].theAnswer[2];
  // var correctAnswer = allQuestions[randomNr].correctAnswer;
  // Prompt the question with the possible answers.
  var answer = prompt(question + '\n' + '0: ' + answer1 + '\n' + '1: ' + answer2 + '\n' + '2: ' + answer3);
  // Check if the answer is correct.
  allQuestions[randomNr].checkAnswer(answer)
}
<button onclick="randomQuestion()">Give me a question!</button>

【问题讨论】:

  • this.correctAnswer = 0; givenAnswer = "0"0 === "0" 为假。 prompt() 返回一个字符串,即使你输入了一个数字。
  • 我认为`如果(this.correctAnswer === givenAnswer)`这就是问题
  • @NiettheDarkAbsol 这很有用!我现在明白我哪里出错了,谢谢!多亏了这个解释,我意识到我可以这样写:this.correctAnswer == giveAnswer,它会起作用的!再次感谢!
  • 可以使用==,是的,但你应该使用parseInt(...)将用户的输入显式转换为数字并继续使用===
  • 太棒了,将我的函数调用更改为 allQuestions[randomNr].checkAnswer(parseInt(answer)),这就像一个魅力。 ??????谢谢!

标签: javascript function if-statement function-constructor


【解决方案1】:

givenAnswer 转换为数字并进行比较 - if (this.correctAnswer === +givenAnswer)

// Build function constructor for the questions with inside: the question, the answers and the correct answer.
function Question(question, [answer1, answer2, answer3], correctAnswer) {
  // Add an instance to Question to count the total amount of questions.
  Question.instances++;
  // Create the blueprint for the questions
  this.theQuestion = question;
  this.theAnswer = [answer1, answer2, answer3];
  this.correctAnswer = correctAnswer;
  // Check if the answer given is correct
  this.checkAnswer = function(givenAnswer) {
    console.log(this.correctAnswer + ' ' + givenAnswer);
    if (this.correctAnswer === +givenAnswer) {
      alert('Well done, that answer is correct!');
    } else {
      alert('Sorry, but that is NOT correct!');
    };
  }
}
// Set the total amount of questions to 0
Question.instances = 0;

// Create an empty array to store the questions in
var allQuestions = [];

// Create a couple questions using the Question function constructor
var q0 = new Question('What is my name?', ['Herman', 'Peter', 'Sander'], 0);
var q1 = new Question('How old am I?', [23, 32, 36], 1);
var q2 = new Question('What is the name of my daugther?', ['Julia', 'Sandra', 'Marijke'], 1);
var q3 = new Question('What is the name of my wife?', ['Esther', 'Marijke', 'Vladlena'], 2);

// Push the question to the empty Array
allQuestions.push(q0);
allQuestions.push(q1);
allQuestions.push(q2);
allQuestions.push(q3);

// Create a function that generates a random question into prompt and checks if the answer is correct
function randomQuestion() {
  var randomNr = Math.floor(Math.random() * Question.instances); // Give a random number based on the amount of questions
  var question = allQuestions[randomNr].theQuestion; // Random question based on the number generated
  // Set the possible answers.
  var answer1 = allQuestions[randomNr].theAnswer[0];
  var answer2 = allQuestions[randomNr].theAnswer[1];
  var answer3 = allQuestions[randomNr].theAnswer[2];
  // var correctAnswer = allQuestions[randomNr].correctAnswer;
  // Prompt the question with the possible answers.
  var answer = prompt(question + '\n' + '0: ' + answer1 + '\n' + '1: ' + answer2 + '\n' + '2: ' + answer3);
  // Check if the answer is correct.
  allQuestions[randomNr].checkAnswer(answer)
}
<button onclick="randomQuestion()">Give me a question!</button>

【讨论】:

    【解决方案2】:

    比较时通过Number() 传递您的输入。检查sn-p

    // Build function constructor for the questions with inside: the question, the answers and the correct answer.
    function Question(question, [answer1, answer2, answer3], correctAnswer) {
      // Add an instance to Question to count the total amount of questions.
      Question.instances++;
      // Create the blueprint for the questions
      this.theQuestion = question;
      this.theAnswer = [answer1, answer2, answer3];
      this.correctAnswer = correctAnswer;
      // Check if the answer given is correct
      this.checkAnswer = function(givenAnswer) {
        console.log(this.correctAnswer + ' ' + givenAnswer);
        if (this.correctAnswer === +Number(givenAnswer)) {
          alert('Well done, that answer is correct!');
        } else {
          alert('Sorry, but that is NOT correct!');
        };
      }
    }
    // Set the total amount of questions to 0
    Question.instances = 0;
    
    // Create an empty array to store the questions in
    var allQuestions = [];
    
    // Create a couple questions using the Question function constructor
    var q0 = new Question('What is my name?', ['Herman', 'Peter', 'Sander'], 0);
    var q1 = new Question('How old am I?', [23, 32, 36], 1);
    var q2 = new Question('What is the name of my daugther?', ['Julia', 'Sandra', 'Marijke'], 1);
    var q3 = new Question('What is the name of my wife?', ['Esther', 'Marijke', 'Vladlena'], 2);
    
    // Push the question to the empty Array
    allQuestions.push(q0);
    allQuestions.push(q1);
    allQuestions.push(q2);
    allQuestions.push(q3);
    
    // Create a function that generates a random question into prompt and checks if the answer is correct
    function randomQuestion() {
      var randomNr = Math.floor(Math.random() * Question.instances); // Give a random number based on the amount of questions
      var question = allQuestions[randomNr].theQuestion; // Random question based on the number generated
      // Set the possible answers.
      var answer1 = allQuestions[randomNr].theAnswer[0];
      var answer2 = allQuestions[randomNr].theAnswer[1];
      var answer3 = allQuestions[randomNr].theAnswer[2];
      // var correctAnswer = allQuestions[randomNr].correctAnswer;
      // Prompt the question with the possible answers.
      var answer = prompt(question + '\n' + '0: ' + answer1 + '\n' + '1: ' + answer2 + '\n' + '2: ' + answer3);
      // Check if the answer is correct.
      allQuestions[randomNr].checkAnswer(answer)
    }
    <button onclick="randomQuestion()">Give me a question!</button>

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      相关资源
      最近更新 更多