gpt4 book ai didi

javascript - 无法在 Javascript 测验中显示最终分数

转载 作者:行者123 更新时间:2023-11-30 09:52:04 25 4
gpt4 key购买 nike

我是 Javascript 的初学者我想创建一个带有单选按钮的基本 javascript 测验应用程序。在测验结束时,我想要一 strip 有最终分数的提醒消息。我无法显示最终得分。这就是我编码的内容..HTML:

   var allQuestions = [{
question: "Who is the Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer: 0
},

{
question: "Who is the Prime Minister of India?",
choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"],
correctAnswer: 2
},

{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"],
correctAnswer: 2
}
];

var correctAnswers = 0;
$(document).ready(function() {
var currentquestion = 0;

$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");

$("#next").click(function() {
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
};
currentquestion++;
if (currentquestion < allQuestions.length) {
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
if (currentquestion == allQuestions.length - 1) {
$('#next').html("Submit");
$('#next').click(function() {
/*If I comment out the following if statement, but retain correctAnswers++, I do get alert message,but the result is obviously wrong*/
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
}
alert(correctAnswers);
//alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers*100/currentquestion) + "%");
});

}

};
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<body>
<div>
<h3 id="question"></h3>
<form id="form">
<input type="radio" name="option" value="0" id="option0" checked>
<label for='option0'>
<br/>
</label>
<input type="radio" name="option" value="1" id="option1">
<label for='option1'>
<br/>
</label>
<input type="radio" name="option" value="2" id="option2">
<label for='option2'>
<br/>
</label>
</form>
</div>
<br/>
<a href="#" id="next" class="button hvr-bounce-to-right">Next</a>

</body>

求助!这是 JSFiddle .

最佳答案

这里有一个工作演示。

var allQuestions = [{
question: "Who is the Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer: 0
},

{
question: "Who is the Prime Minister of India?",
choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"],
correctAnswer: 2
},

{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"],
correctAnswer: 2
}
];


$(document).ready(function() {
var currentquestion = 0;
var correctAnswers = 0;
var done = false;

$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");

$("#next").click(function() {
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
};

if (done) {
alert(correctAnswers);
} else {
currentquestion++;
if (currentquestion < allQuestions.length) {
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
if (currentquestion == allQuestions.length - 1) {
$('#next').html("Submit");
done = true;
}
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<body>
<div>
<h3 id="question"></h3>
<form id="form">
<input type="radio" name="option" value="0" id="option0" checked>
<label for='option0'>
<br/>
</label>
<input type="radio" name="option" value="1" id="option1">
<label for='option1'>
<br/>
</label>
<input type="radio" name="option" value="2" id="option2">
<label for='option2'>
<br/>
</label>
</form>
</div>
<br/>
<a href="#" id="next" class="button hvr-bounce-to-right">Next</a>

</body>

关于javascript - 无法在 Javascript 测验中显示最终分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com