我正在尝试使用javascript构建一个多项选择测试,它一次显示一个问题(淡入下一个问题).在所有问题都得到解答后,根据您的答案,它会为您提供结果.实际问题的结果不需要仅显示该分数的分数和解释,因为结果将是一堆文本.
例如:
下面是我的代码,如何更改它以便没有答案是"正确的"它只是在测验结束时根据上述分数的范围给出文本结果?
使用Javascript:
(function() {
var questions = [{
question: "Question1",
choices: ["choice1", "choice2", "choice3"],
correctAnswer:0
}, {
question: "Question2",
choices: ["choice1", "choice2", "choice3"],
correctAnswer: 4
}, {
question: "Question3",
choices: ["choice1", "choice2", "choice3"],
correctAnswer: 0
}, {
question: "Question4",
choices: ["choice1", "choice2", "choice3"]
correctAnswer: 3
}, {
question: "Question5",
choices: ["choice1", "choice2", "choice3"],
correctAnswer: 4
}];
var questionCounter = 0; //Tracks question number
var selections = []; //Array containing user choices
var quiz = $('#quiz'); //Quiz div object
// Display initial question
displayNext();
// Click handler for the 'next' button
$('#next').on('click', function (e) {
e.preventDefault();
// Suspend click listener during fade animation
if(quiz.is(':animated')) {
return false;
}
choose();
// If no user selection, progress is stopped
if (isNaN(selections[questionCounter])) {
alert('Please make a selection!');
} else {
questionCounter++;
displayNext();
}
});
// Click handler for the 'prev' button
$('#prev').on('click', function (e) {
e.preventDefault();
if(quiz.is(':animated')) {
return false;
}
choose();
questionCounter--;
displayNext();
});
// Click handler for the 'Start Over' button
$('#start').on('click', function (e) {
e.preventDefault();
if(quiz.is(':animated')) {
return false;
}
questionCounter = 0;
selections = [];
displayNext();
$('#start').hide();
});
// Animates buttons on hover
$('.button').on('mouseenter', function () {
$(this).addClass('active');
});
$('.button').on('mouseleave', function () {
$(this).removeClass('active');
});
// Creates and returns the div that contains the questions and
// the answer selections
function createQuestionElement(index) {
var qElement = $('<div>', {
id: 'question'
});
var header = $('<h2>Question ' + (index + 1) + '</h2>');
qElement.append(header);
var question = $('<p>').append(questions[index].question);
qElement.append(question);
var radioButtons = createRadios(index);
qElement.append(radioButtons);
return qElement;
}
// Creates a list of the answer choices as radio inputs
function createRadios(index) {
var radioList = $('<ul>');
var item;
var input = '';
for (var i = 0; i < questions[index].choices.length; i++) {
item = $('<li>');
input = '<input type="radio" name="answer" value=' + i + ' />';
input += questions[index].choices[i];
item.append(input);
radioList.append(item);
}
return radioList;
}
// Reads the user selection and pushes the value to an array
function choose() {
selections[questionCounter] = +$('input[name="answer"]:checked').val();
}
// Displays next requested element
function displayNext() {
quiz.fadeOut(function() {
$('#question').remove();
if(questionCounter < questions.length){
var nextQuestion = createQuestionElement(questionCounter);
quiz.append(nextQuestion).fadeIn();
if (!(isNaN(selections[questionCounter]))) {
$('input[value='+selections[questionCounter]+']').prop('checked', true);
}
// Controls display of 'prev' button
if(questionCounter === 1){
$('#prev').show();
} else if(questionCounter === 0){
$('#prev').hide();
$('#next').show();
}
}else {
var scoreElem = displayScore();
quiz.append(scoreElem).fadeIn();
$('#next').hide();
$('#prev').hide();
$('#start').show();
}
});
}
// Computes score and returns a paragraph element to be displayed
function displayScore() {
var score = $('<p>',{id: 'question'});
var numCorrect = 0;
for (var i = 0; i < selections.length; i++) {
if (selections[i] === questions[i].correctAnswer) {
numCorrect++;
}
}
score.append('You got ' + numCorrect + ' questions out of ' +
questions.length + ' right!!!');
return score;
}
})();
Run Code Online (Sandbox Code Playgroud)
HTML:
<div class="col-md-6" style="padding:10px;">
<h4 class="text-center lead" style="color:#333;font-size:28px;"> Relationship Test </h4>
<div class="test">
<a id="update" href="index.html" class="pull-right"><i style="color:#333;" class="glyphicon glyphicon-repeat glyphicon-repeat-animate"></i></a>
<p id="quiz" class="lead quiz-1" style="color:#333;padding:20px;"> </p>
</div>
<div style="margin-top:5px;">
<a type="" id="next" href="#tellme" class="button btn btn-block testbuttons" onclick="" >Next </a>
<a type="" id="prev" href="#tellme" class="button btn btn-block testbuttons" onclick="" >Previous </a>
<a type="" id="start" href="#tellme" class="button btn btn-block testbuttons" onclick="" >Start Over </a>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我分叉了你的笔:http://codepen.io/anon/pen/XXKeaN 我想我已经修好了。
请看一下答案,它们对选择具有价值。checkAnswer() 函数计算这一点,特别是这一行
score = score + quiz[currentQuestion].answer[i];
Run Code Online (Sandbox Code Playgroud)
输出在最后,只显示一个单一的分数。
| 归档时间: |
|
| 查看次数: |
944 次 |
| 最近记录: |