Javascript 根据答案是否正确,分数计算不正确

Javascript 根据答案是否正确,分数计算不正确,javascript,jquery,html,Javascript,Jquery,Html,我很确定我错过了一些简单的东西,但分数并不是根据是否选择了正确的答案来计算的。这是一个选择题测验。代码是由其他人编写的,所以请尝试独自解决此问题!有什么想法吗 var问题=函数(文本){ 这个问题=文本; 答案=[]; this.addAnswer=函数(答案,正确){ 如果(typeof Correct=='undefined')Correct=false; else Correct=正确==正确; this.Answers.push({“Answer”:Answer,“Correct”:C

我很确定我错过了一些简单的东西,但分数并不是根据是否选择了正确的答案来计算的。这是一个选择题测验。代码是由其他人编写的,所以请尝试独自解决此问题!有什么想法吗


var问题=函数(文本){
这个问题=文本;
答案=[];
this.addAnswer=函数(答案,正确){
如果(typeof Correct=='undefined')Correct=false;
else Correct=正确==正确;
this.Answers.push({“Answer”:Answer,“Correct”:Correct});
};
this.checkAnswer=函数(索引){
if(typeof this.Answers[Index]=“undefined”)返回false;
返回此。答案[Index]。正确;
};
};
var问题=[];
var-q;
var得分=0;
var CurrentQuestion=-1;
var nextQuestion=函数()
{
++当前问题;
如果(问题类型[CurrentQuestion]=“undefined”)返回false;
var问题=问题[当前问题];
$('#QuestionIndex').html(数字[CurrentQuestion]);
$('#QuestionsRemaining').html((Questions.length-CurrentQuestion-1));
$('Question').html(Question.Question);
$('#Answers').html('');
for(问题中的var t.答案)
{
i=t;
t=问题。答案[t];
var Div=$('');
$(Div).附加(“”);
$(Div.append($('').html(t.Answer));
$(“#答案”)。追加(Div);
}
$(“输入[type='radio']”)。更改(函数(){
$('.radioContainer').css('background-image','none');
attr=$(this.attr('checked');
if(类型属性!='undefined'&&attr!==false)
{
$(this).parent().css('background-image','url('http://dev.clickymedia.co.uk/fbart/wp-content/themes/artbook/img/tick.png")');
}
});
返回true;
}
//返回false将阻止用户继续下一个问题。
var checkCurrentAnswer=函数()
{
如果(问题类型[CurrentQuestion]=“undefined”)返回false;
var i=$(“#回答输入:选中”).parent().index();
如果(i<0)返回false;
如果(问题[CurrentQuestion]。检查答案(i)==true)得分++;
返回true;
}
q=新问题(“流行歌手安迪·沃霍尔(Andy Warhol)为哪位著名电影明星拍的肖像画最多?”);
q、 addAnswer(“玛丽莲·梦露”,真);
q、 addAnswer(“索菲亚·罗兰”);
q、 addAnswer(“奥黛丽·赫本”);
q、 addAnswer(“多丽丝·戴”);
问题。推(q);
q=新问题(“列奥纳多·达芬奇的代表作《蒙娜丽莎》将人群吸引到哪个著名的欧洲博物馆?”);
q、 addAnswer(“巴黎卢浮宫博物馆”,真实);
q、 addAnswer(“泰特英国,伦敦”);
q、 addAnswer(“柏林国家美术馆”);
q、 addAnswer(“佛罗伦萨乌菲齐画廊”);
问题。推(q);
$(函数(){
nextQuestion();
$('#nextQuestion').bind('click',function(){
if(checkCurrentAnswer())
{
if(nextQuestion())
{
}否则{//考试结束了!-放下你的钢笔和铅笔。
$('#QuestionBook.FinalLeft.Score').html(Score+'/'+Questions.length);
$('#QuestionBook.FinalLeft.Message').html(Message);
$('#QuestionBook.QuestionContainer,#QuestionBook.answerscocontainer').hide();
$('#QuestionBook.FinalLeft,#QuestionBook.FinalRight').show();
}
}
});
});


您的
checkCurrentAnswer
函数计算的索引错误。具体而言,这一行:

var i=$(“#回答输入:选中”).parent().index()

您的HTML如下所示:

<div id="Answers">
  <div class="answer">
    <span class="radioContainer"><input type="radio" /></span>
    <label for="answer_0">The Musée du Louvre, Paris</label>
  </div>
  <div class="answer">
    <span class="radioContainer"><input type="radio" /></span>
    <label for="answer_1">The Tate Britain, London</label>
  </div>
  ...
</div>

这是大量的代码,而您并没有给我们提供太多的支持。“数错”是什么意思?复制的步骤?有什么不对的?您也可以尝试删除尽可能多的代码,并得出一个。每个问题的分数都在增加,而分数只有在答案正确时才应该增加。遗憾的是,我没有多少可以略过的,因为很多函数都是相对的…不幸的是,以这种方式构建它不是我的选择,因为我知道有很多更简单的方法+感谢你花时间检查所有代码。做得好。我不知道你的答案是否正确,但你的努力做得很好。很明显,你在回答他们的问题时比OP投入了更多的精力。非常好!谢谢有时候,这只是其中的一种情况,你必须拿出比正常多一点的代码,因为这不是我认为问题所在的地方!谢谢!:)
<div id="Answers">
  <div class="answer">
    <span class="radioContainer"><input type="radio" /></span>
    <label for="answer_0">The Musée du Louvre, Paris</label>
  </div>
  <div class="answer">
    <span class="radioContainer"><input type="radio" /></span>
    <label for="answer_1">The Tate Britain, London</label>
  </div>
  ...
</div>
var i = $('#Answers input:checked').closest(".answer").index();