问题描述
我正在尝试使用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,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>
解决方法
将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,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
是索引,您正在比较该索引,而不是值。
更改:
if (this.correctAnswer === givenAnswer) {
收件人
if (this.theAnswer[this.correctAnswer] === givenAnswer) {
// Build function constructor for the questions with inside: the question,answer3];
this.correctAnswer = correctAnswer;
// Check if the answer given is correct
this.checkAnswer = function(givenAnswer) {
console.log(this.theAnswer[this.correctAnswer] + ' ' + givenAnswer);
if (this.theAnswer[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?',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>
,
比较时,将输入的内容通过Number()
。检查代码段
// Build function constructor for the questions with inside: the question,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,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>