└── quiz_page ├── jsquiz.js ├── stylesheet.css └── web1.html /quiz_page/jsquiz.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | var questions = [{ 3 | question: "What is 2*5?", 4 | choices: [2, 5, 10, 15, 20], 5 | correctAnswer: 2 6 | }, { 7 | question: "What is 3*6?", 8 | choices: [3, 6, 9, 12, 18], 9 | correctAnswer: 4 10 | }, { 11 | question: "What is 8*9?", 12 | choices: [72, 99, 108, 134, 156], 13 | correctAnswer: 0 14 | }, { 15 | question: "What is 1*7?", 16 | choices: [4, 5, 6, 7, 8], 17 | correctAnswer: 3 18 | }, { 19 | question: "What is 8*8?", 20 | choices: [20, 30, 40, 50, 64], 21 | correctAnswer: 4 22 | }]; 23 | 24 | var questionCounter = 0; //Tracks question number 25 | var selections = []; //Array containing user choices 26 | var quiz = $('#quiz'); //Quiz div object 27 | 28 | // Display initial question 29 | displayNext(); 30 | 31 | // Click handler for the 'next' button 32 | $('#next').on('click', function (e) { 33 | e.preventDefault(); 34 | 35 | // Suspend click listener during fade animation 36 | if(quiz.is(':animated')) { 37 | return false; 38 | } 39 | choose(); 40 | 41 | // If no user selection, progress is stopped 42 | if (isNaN(selections[questionCounter])) { 43 | alert('Please make a selection!'); 44 | } else { 45 | questionCounter++; 46 | displayNext(); 47 | } 48 | }); 49 | 50 | // Click handler for the 'prev' button 51 | $('#prev').on('click', function (e) { 52 | e.preventDefault(); 53 | 54 | if(quiz.is(':animated')) { 55 | return false; 56 | } 57 | choose(); 58 | questionCounter--; 59 | displayNext(); 60 | }); 61 | 62 | // Click handler for the 'Start Over' button 63 | $('#start').on('click', function (e) { 64 | e.preventDefault(); 65 | 66 | if(quiz.is(':animated')) { 67 | return false; 68 | } 69 | questionCounter = 0; 70 | selections = []; 71 | displayNext(); 72 | $('#start').hide(); 73 | }); 74 | 75 | // Animates buttons on hover 76 | $('.button').on('mouseenter', function () { 77 | $(this).addClass('active'); 78 | }); 79 | $('.button').on('mouseleave', function () { 80 | $(this).removeClass('active'); 81 | }); 82 | 83 | // Creates and returns the div that contains the questions and 84 | // the answer selections 85 | function createQuestionElement(index) { 86 | var qElement = $('
', { 87 | id: 'question' 88 | }); 89 | 90 | var header = $('

Question ' + (index + 1) + ':

'); 91 | qElement.append(header); 92 | 93 | var question = $('

').append(questions[index].question); 94 | qElement.append(question); 95 | 96 | var radioButtons = createRadios(index); 97 | qElement.append(radioButtons); 98 | 99 | return qElement; 100 | } 101 | 102 | // Creates a list of the answer choices as radio inputs 103 | function createRadios(index) { 104 | var radioList = $('