var questions = [
{
question:"How boring is this quiz?",
choices:["kinda","very","Too boring","Very fun"],
correctAnswer: 0
},
{
question:"What color is Mace Windu's Lightsaber?",
choices:["purple", "blue", "green", "yellow"],
correctAnswer: 0
},
{
question:"Who purchased ATI in 2006?",
choices:['Asus', 'NZXT', 'AMD', 'HyperX'],
correctAnswer: 2
},
{
question:"What is the rest of this Star Wars characters name? Jar Jar...",
choices:["Smith", "Ranks", "Banks", "Johnson"],
correctAnswer: 2
},
{
question:"What color is C3PO?",
choices:["Golden", "Blue", "Orange", "Teal"],
correctAnswer: 0
},
{
question:"What is missing from the quote? These are not the ____ you're looking for.",
choices:["Shirts", "Movies", "Computers", "Droids"],
correctAnswer: 3
},
{
question:"What is the correct version of the characters name that starts with Jabba?",
choices:["jabba The Small", "Jabba Williams", "Jabba The Hutt", "Jabba The Clean"],
correctAnswer: 2
},
{
question:"Which answer has the correct spelling of a characters name?",
choices:["Fiin", "Finn", "Fiinn", "Fin"],
correctAnswer: 1
},
{
question:"Which of the following is not a character in Star Wars?",
choices:["Luke Skywalker", "Finn", "R2D2", "Morgan Skywalker"],
correctAnswer: 3
},
{
question:"Is the word yes an answer for this question?",
choices:["yes", "This is the wrong answer", "This is the wrong answer", "This is the wrong answer"],
correctAnswer: 0
}
];
$(document).ready(function(){
// var numCorrect = 0;
var myNumber = 0;
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 () {
// 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();
}
return false;
});
// 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 () {
if(quiz.is(':animated')) {
return false;
}
questionCounter = 0;
selections = [];
displayNext();
$('#start').hide();
return false;
});
// Creates and returns the div that contains the questions and
// the answer selections
function createQuestionElement(index) {
var qElement = $('<div>', {
id: 'question'
});
var header = $('<h3>Question ' + (index + 1) + ':</h3>');
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();
$("#info").html("Number of correct ones: " + correctOnes());
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();
}
});
}
function displayScore() {
var numCorrect = 0;
var score = $('<p>',{id: 'question'});
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;
}
function correctOnes() {
var numCorrect = 0;
var score = $('<p>',{id: 'question'});
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 numCorrect;
}
});
a{
text-decoration: none;
}
h1 {
text-align: center;
font-size: 45px;
font-family: cursive;
color: teal;
text-shadow: 2px 1px black;
}
ul {
margin-right: auto;
margin-left: auto;
}
li {
list-style: none;
}
.radiochoices{
font-family: comic sans ms;
color: white;
font-size: 20px;
}
#container {
margin:auto;
width: 50%;
padding: 0px 0px 30px 0px;
background-color:#008080;
border:4px solid #B0E0E6;
border-radius: 13px;
color:#000000;
font-weight: bold;
box-shadow: 5px 5px 10px #888;
}
.center {
margin: 205px 40px 0px 245px
}
.reset {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div id='container'>
<div id='quiz'></div>
<button class="btn btn-info btn-large" id='next'><a href='#'>Next</a></button>
<button class="btn btn-warning btn-large reset" id='prev'><a href='#'>Prev</a></button>
<button class="btn btn-info btn-large" id='start'> <a href='#'>Start Over</a></button>
<div id = "info"></div>
</div>
我是一名优秀的程序员,十分优秀!