簡體   English   中英

該函數沒有給我正確的結果

[英]The function doesn't give me the correct result

我正在嘗試在 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>

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>

比較時通過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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM