-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (140 loc) · 4.18 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// initialize variables
var questionID, question, choiceA, choiceB, choiceC, choiceD, userChoice, questions, numQuestions, qInfo,
current = 0,
score = 0,
points = [];
var defaultQuestions = [
{
question: "Where are the three smallest bones in the human body?",
choiceA: "middle ear",
choiceB: "nose",
choiceC: "toes",
choiceD: "eyes",
correct: "A"
},
{
question: "What is the most abundant element in the Universe?",
choiceA: "Helium",
choiceB: "Oxygen",
choiceC: "Lithium",
choiceD: "Hydrogen",
correct: "D"
},
{
question: "Approximately how long does it take for light to travel from the Sun's surface to the Earth?",
choiceA: "8 days",
choiceB: "8 seconds",
choiceC: "8 minutes",
choiceD: "8 hours",
correct: "C"
},
{
question: "What is 10/2?",
choiceA: "5",
choiceB: "2",
choiceC: "8",
choiceD: "9",
correct: "A"
},
{
question: "Which planet has the most moons?",
choiceA: "Saturn",
choiceB: "Mars",
choiceC: "Jupiter",
choiceD: "Uranus",
correct: "C"
}
];
var elQuiz = document.getElementById("quiz");
var elQuizStatus = document.getElementById("quizStatus");
var elQuestion = document.getElementById("question");
var elChoiceA = document.getElementById("choiceA");
var elChoiceB = document.getElementById("choiceB");
var elChoiceC = document.getElementById("choiceC");
var elChoiceD = document.getElementById("choiceD");
var elChoices = document.getElementsByName('choices');
// start quiz
populateQuestions();
renderQuestion();
document.getElementById("submit").onclick = gradeQuestion;
function populateQuestions(){
// populate with default questions
questions = defaultQuestions;
// if local storage contains questions, add to question set
if(localStorage.getItem("questions")){
var storedQuestions = JSON.parse(localStorage.getItem("questions"));
for(var i = 0; i < storedQuestions.length; i++){
questions.push(storedQuestions[i]);
}
}
numQuestions = questions.length;
}
function populateQuestionInfo(){
question = questions[current].question;
qInfo = questions[current];
choiceA = qInfo.choiceA;
choiceB = qInfo.choiceB;
choiceC = qInfo.choiceC;
choiceD = qInfo.choiceD;
correct = qInfo.correct;
}
function renderQuestion(){
questionID = current + 1;
elQuizStatus.innerHTML = "Question " + (questionID) + " of " + (numQuestions);
populateQuestionInfo();
elQuestion.innerHTML = question;
elChoiceA.innerHTML = choiceA;
elChoiceB.innerHTML = choiceB;
elChoiceC.innerHTML = choiceC;
elChoiceD.innerHTML = choiceD;
}
function gradeQuestion(){
if(getUserChoice()){
if(userChoice == questions[current].correct){
score++;
points[current] = 1;
}
else{
points[current] = 0;
}
if(current == questions.length-1){
endGame();
}
else{
// next question
current++;
renderQuestion();
}
}
}
function getUserChoice(){
for (var i = 0, length = elChoices.length; i < length; i++)
{
if (elChoices[i].checked)
{
userChoice = elChoices[i].value;
// clear radio input for next question
elChoices[i].checked = false;
return true;
}
}
// user didn't select an answer
alert("Please select an answer before continuing");
return false;
}
function endGame(){
elQuiz.innerHTML = "<h2>Your Score: " + score + " out of " + numQuestions + "</h2>";
for(var i = 0; i < points.length; i++){
var summary = document.createElement("p");
if(points[i] == 0){
summary.innerHTML = "Question #" + (i+1) + ": INCORRECT";
summary.style.color = "red";
}
else{
summary.innerHTML = "Question #" + (i+1) + ": CORRECT!";
summary.style.color = "green";
}
elQuiz.appendChild(summary);
}
document.getElementById("options").style.display = "block";
}