1

我正在尝试在 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>

4

2 回答 2

0

在比较时通过您的输入Number()。检查片段

// 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>

于 2020-10-25T08:10:42.793 回答
0

转换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>

于 2020-10-25T07:55:57.603 回答