|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | + <title>Quiz Challenge</title> |
| 6 | + <link rel="stylesheet" type="text/css" href="style.css"> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | + <div class="quiz_wrapper"> |
| 10 | + <div class="question" id="questionBox"> |
| 11 | + |
| 12 | + </div> |
| 13 | + |
| 14 | + <div class="options"> |
| 15 | + <ul id="ul"> |
| 16 | + <li id="opt1" onclick="button(this)"></li> |
| 17 | + <li id="opt2" onclick="button(this)"></li> |
| 18 | + <li id="opt3" onclick="button(this)"></li> |
| 19 | + <li id="opt4" onclick="button(this)"></li> |
| 20 | + </ul> |
| 21 | + </div> |
| 22 | + <div class="score"> |
| 23 | + <div class="next"> |
| 24 | + <button type="button" onclick="moveNext()" id="nextButton">Next</button> |
| 25 | + </div> |
| 26 | + <div class="score-card"> |
| 27 | + Points : <span id="scoreCard">0</span> |
| 28 | + </div> |
| 29 | + <div class="back"> |
| 30 | + <button type="button" onclick="moveBack()" id="backButton">Back</button> |
| 31 | + </div> |
| 32 | + <div class="restart"> |
| 33 | + <button type="button" onclick="reloadPage()" id="restartButton">Restart</button> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | + |
| 37 | + |
| 38 | + </div> |
| 39 | + <script type="text/javascript"> |
| 40 | + function reloadPage() { |
| 41 | + location.reload(); |
| 42 | + } |
| 43 | + </script> |
| 44 | + |
| 45 | + |
| 46 | + <script type="text/javascript"> |
| 47 | + |
| 48 | + var ul = document.getElementById('ul'); |
| 49 | + var nextBtn = document.getElementById('nextButton'); |
| 50 | + var scoreCard = document.getElementById('scoreCard'); |
| 51 | + var quizBox = document.getElementById('questionBox'); |
| 52 | + var opt1 = document.getElementById('opt1'); |
| 53 | + var opt2 = document.getElementById('opt2'); |
| 54 | + var opt3 = document.getElementById('opt3'); |
| 55 | + var opt4 = document.getElementById('opt4'); |
| 56 | + |
| 57 | + |
| 58 | + var app = { |
| 59 | + questions: [ |
| 60 | + { q: 'What does CPU stand for?', options: ['Central Processing Unit', 'Computer Personal Unit', 'Central Personal Unit', 'None of these'], answer: 1 }, |
| 61 | + { q: 'What does RAM stand for?', options: ['Read After Modification', 'Random Access Memory', 'Random After Modification', 'Read Access Memory'], answer: 2 }, |
| 62 | + |
| 63 | + { q: 'Which company developed JavaScript?', options: ['Oracle', 'Netscape', 'Microsoft', 'Google'], answer: 2 }, |
| 64 | + |
| 65 | + { q: 'What is the default value of the opacity property in CSS?', options: ['0.5', '1', '0', '0.1'], answer: 3 }, |
| 66 | + |
| 67 | + { q: 'How do you call a function named "myFunction" in JavaScript?', options: ['call myFunction()', 'call function myFunction()', 'myFunction()', 'function myFunction()'], answer: 3 }, |
| 68 | + |
| 69 | + ], |
| 70 | + index: 0, |
| 71 | + load: function () { |
| 72 | + if (this.index <= this.questions.length - 1) { |
| 73 | + quizBox.innerHTML = this.index + 1 + ". " + this.questions[this.index].q; |
| 74 | + opt1.innerHTML = this.questions[this.index].options[0]; |
| 75 | + opt2.innerHTML = this.questions[this.index].options[1]; |
| 76 | + opt3.innerHTML = this.questions[this.index].options[2]; |
| 77 | + opt4.innerHTML = this.questions[this.index].options[3]; |
| 78 | + this.scoreCard(); |
| 79 | + } |
| 80 | + else { |
| 81 | + |
| 82 | + quizBox.innerHTML = "Quiz Finished......!!!" |
| 83 | + opt1.style.display = "none"; |
| 84 | + opt2.style.display = "none"; |
| 85 | + opt3.style.display = "none"; |
| 86 | + opt4.style.display = "none"; |
| 87 | + nextBtn.style.display = "none"; |
| 88 | + } |
| 89 | + }, |
| 90 | + moveNext: function () { |
| 91 | + this.index++; |
| 92 | + this.load(); |
| 93 | + }, |
| 94 | + |
| 95 | + moveBack: function () { |
| 96 | + this.index--; |
| 97 | + this.load(); |
| 98 | + }, |
| 99 | + checkAnswer: function (ele) { |
| 100 | + |
| 101 | + var id = ele.id.split(''); |
| 102 | + |
| 103 | + if (id[id.length - 1] == this.questions[this.index].answer) { |
| 104 | + this.score++; |
| 105 | + ele.className = "correct"; |
| 106 | + ele.innerHTML = "Correct"; |
| 107 | + this.scoreCard(); |
| 108 | + } |
| 109 | + else { |
| 110 | + ele.className = "wrong"; |
| 111 | + ele.innerHTML = "Wrong"; |
| 112 | + } |
| 113 | + }, |
| 114 | + notClickable: function () { |
| 115 | + for (let i = 0; i < ul.children.length; i++) { |
| 116 | + ul.children[i].style.pointerEvents = "none"; |
| 117 | + } |
| 118 | + }, |
| 119 | + |
| 120 | + clickable: function () { |
| 121 | + for (let i = 0; i < ul.children.length; i++) { |
| 122 | + ul.children[i].style.pointerEvents = "auto"; |
| 123 | + ul.children[i].className = '' |
| 124 | + |
| 125 | + } |
| 126 | + }, |
| 127 | + score: 0, |
| 128 | + scoreCard: function () { |
| 129 | + scoreCard.innerHTML = this.questions.length + "/" + this.score; |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + window.onload = app.load(); |
| 135 | + |
| 136 | + function button(ele) { |
| 137 | + app.checkAnswer(ele); |
| 138 | + app.notClickable(); |
| 139 | + } |
| 140 | + |
| 141 | + function moveNext() { |
| 142 | + app.moveNext(); |
| 143 | + app.clickable(); |
| 144 | + } |
| 145 | + |
| 146 | + function moveBack() { |
| 147 | + app.moveBack(); |
| 148 | + app.clickable(); |
| 149 | + } |
| 150 | + </script> |
| 151 | +</body> |
| 152 | + |
| 153 | +</html> |
0 commit comments