Javascript 比较两个函数的返回值(动态测试)
背景 我正在创建一个动态的、用JavaScript编写的多项选择题测验,作为这项工作的高潮。我正在遵循本课程作者在后续课程中概述的寄生继承模式,并且在完成项目时遇到了困难 问题 如果您参考这个,您将看到我已经创建了loadQuestion、displayQuestion、getUserAnswer、getCorrectAnswer和checkAnswer的函数。检查答案是我被卡住的地方。在这个函数中,我想检查getUserAnswer的返回值是否等于当前加载的问题上getCorrectAnswer的返回值,如果是,现在只需在控制台上记录一个字符串Javascript 比较两个函数的返回值(动态测试),javascript,Javascript,背景 我正在创建一个动态的、用JavaScript编写的多项选择题测验,作为这项工作的高潮。我正在遵循本课程作者在后续课程中概述的寄生继承模式,并且在完成项目时遇到了困难 问题 如果您参考这个,您将看到我已经创建了loadQuestion、displayQuestion、getUserAnswer、getCorrectAnswer和checkAnswer的函数。检查答案是我被卡住的地方。在这个函数中,我想检查getUserAnswer的返回值是否等于当前加载的问题上getCorrectAnswe
Question.prototype.displayQuestion = function(){
var questionToDisplay = '<div class="question">' + this.question + '</div><ul>';
choiceCounter = 0;
var quizDiv = document.getElementById('quiz');
this.choices.forEach(function(eachChoice){
questionToDisplay += '<li><input type="radio" name="choice" value="' + choiceCounter + '">' + eachChoice + '</li>';
choiceCounter++;
});
questionToDisplay += '</ul>';
quizDiv.innerHTML = questionToDisplay;
};
var i = 0;
Question.prototype.loadQuestion = function(){
if(i < allQuestions.length){
var quest = new MultipleChoiceQuestion(allQuestions[i].question, allQuestions[i].choices, allQuestions[i].correctAnswer);
quest.displayQuestion();
i++;
return quest.getCorrectAnswer();
}
};
Question.prototype.getCorrectAnswer = function() {
return this.correctAnswer;
};
Question.prototype.getUserAnswer = function(){
var radio = document.getElementsByName('choice');
for(var i=0; i < radio.length; i++){
if(radio[i].checked){
return radio[i].value;
}
}
};
//Non-functioning code, this is what im trying to figure out.
/*Question.prototype.checkAnswer = function(){
if(Question.prototype.loadQuestion() == Question.prototype.getUserAnswer()){
console.log('it worked!');
} else {
console.log('keep trying!');
}
};*/
//Load the first question
Question.prototype.loadQuestion();
var button = document.getElementById('next');
button.onclick = function(){
//get the user-selected radio input.
Question.prototype.getUserAnswer();
//Question.prototype.checkAnswer();
//load next question
Question.prototype.loadQuestion();
};
Question.prototype.displayQuestion=function(){
var questionToDisplay=''+this.question+';
choiceCounter=0;
var quizDiv=document.getElementById('quick');
this.choices.forEach(函数(eachChoice){
问题显示+=“- ”+每个选项+”
”;
choiceCounter++;
});
问题显示+=“
”;
quizDiv.innerHTML=问题显示;
};
var i=0;
Question.prototype.loadQuestion=函数(){
如果(i
我不熟悉javascript(和编程),怀疑问题在于对返回值如何工作的误解
谢谢大家! 找到了问题
你没有在收音机[i]的价值上加1。请记住,这是一个数组,因此所有索引都将减少1,因为我们从0开始计数
以下是您的功能:
Question.prototype.getUserAnswer = function(){
var radio = document.getElementsByName('choice');
for(var i=0; i < radio.length; i++){
if(radio[i].checked){
return +(radio[i].value) + 1;
}
}
};
Question.prototype.getUserAnswer=function(){
var radio=document.getElementsByName('choice');
对于(var i=0;i
我现在意识到问题更多的是我的方法,而不是任何一个错误。我模仿并改进了另一个学生的榜样。以下是该项目的现状。我学到的最重要的事情是如何封装变量,我需要所有函数也能访问这些变量。在这种情况下,,一个名为currentQuestionIndex的变量对解决方案非常重要。请将相关的代码片段放到这里的帖子中好吗?我在比较中使用loadQuestion,因为它返回当前加载的multipleEchoiceQuestion实例上application.getCorrectAnswer的值。我的功能正常工作,直到checkAnswer功能。我想我会用更多的代码来编辑更好的上下文。谢谢。我意识到我理解错了你的代码。我发现了问题并编辑了我的答案。这应该可以解决问题。我正在递增displayQuestion函数中的radio[I]值,choiceCounter=0;因此,如果选中了无线电输入,则getUserAnswer将返回无线电输入的值。这样,如果用户检查第一个无线电输入,它将返回0,第二个输入将返回1,依此类推。我使用您发布的代码在我的计算机上尝试了这一点,似乎已经解决了问题。它找到了正确的答案。也许你想试试,看看它是否能解决你所说的问题。我把你的编辑整合到帖子中,问题依然存在。