简体   繁体   中英

The function doesn't give me the correct result

I am trying to build a functionality in JavaScript that shoots random questions into a prompt, together with the 3 possible answers. User gives an answer and the function shows the result (whether the answer is correct or not) in an alertbox. And exactly there I'm experiencing trouble. Because whatever the answer given, is correct or wrong, it always will show that the answer is not correct. I've gone over the code a 100 times, but can't find my mistake.. Can someone help and explain me where I went wrong?

Besides, if there are any improvements possible to the code, without using JQuery, i'd love to hear them! I only started learning JS recently, so ANY input is welcome!

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

Convert the givenAnswer to number and compare - 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>

Pass your input through the Number() when comparing. check the snippet

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM