Я начинающий в Javascript Я хочу создать базовое приложение для викторины javascript с переключателями. В конце викторины я хочу получить сообщение с окончательным счетом. Я не могу отобразить окончательный результат. Это то, что я закодированы еще .. HTML:Невозможно отобразить итоговый счет в викторине Javascript
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.
Это замечательный человек. Не могли бы вы рассказать/объяснить свой ответ? – Rashid
Конечно, когда пользователь нажимает кнопку отправки, переменная correctAnswers уже имеет правильное значение. В этот момент текущее условие равно 3. Таким образом, при сравнении allQuestions [currentquestion] .correctAnswer не определен, и ваш исходный код завершает свое исполнение без успеха. –
Если вы показываете предупреждение с правильными ответами перед или вместо сравнения, вы увидите, что его значение равно 3. alert (correctAnswers) –