-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (115 loc) · 4.55 KB
/
index.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
"use strict"
let questionNum = 0;
let score = 0;
function generateQuestions() {
if (questionNum < questionStore.length) {
$("#nav").html(
`<h3 class="questions">Question ${questionNum + 1}/10</h3>
<h3 class="score">Score ${score}/100</h3>`);
return `
<form>
<fieldset>
<div class="quiz-answer-choices">
<legend>${questionStore[questionNum].question}</legend>
<label>
<input type="radio" name="answerChoices" value="${questionStore[questionNum].answers[0]}" required>
${questionStore[questionNum].answers[0]}
</label>
<label>
<input type="radio" name="answerChoices" value="${questionStore[questionNum].answers[1]}" required>
${questionStore[questionNum].answers[1]}
</label>
<label>
<input type="radio" name="answerChoices" value="${questionStore[questionNum].answers[2]}" required>
${questionStore[questionNum].answers[2]}
</label>
<label>
<input type="radio" name="answerChoices" value="${questionStore[questionNum].answers[3]}" required>
${questionStore[questionNum].answers[3]}
</label>
</div>
<input type="submit" name="submit" id="js-submitBtn" value="SUBMIT">
</fieldset>
<div id="alert"></div>
</form>
</main>
`;
} else {
overallScore();
}
}
function renderQuestion() {
$('#container').html(generateQuestions());
}
function handleStartButton() {
$('#js-startBtn').on('click', function() {
renderQuestion();
});
}
function handleQuestionNum() {
questionNum ++;
}
function handleScore() {
score = score + 10;
$("#nav").html(
`<h3 class="questions">Question ${questionNum + 1}/10</h3>
<h3 class="score">Score ${score}/100</h3>`);
}
function handleSubmitButton() {
$('#container').on('click', '#js-submitBtn', function(event) {
event.preventDefault();
let selected = $('input:checked');
let userAnswer = selected.val();
let correctAnswer = `${questionStore[questionNum].correctAnswer}`;
if (userAnswer === undefined) {
$("#alert").html("You must choose one.")
} else {
if (correctAnswer === userAnswer) {
handleScore();
const correctFeedback = `
<div class="correct-answer">
<h2>Correct!</h2>
<button type="submit" id="js-nextBtn">NEXT</button>
</div>`;
$('#container').html(correctFeedback);
} else {
const incorrectFeedback = `
<div class="wrong-answer">
<h2>Wrong!</h2>
<h3>The correct answer is ${questionStore[questionNum].correctAnswer}!</h3>
<button type="submit" id="js-nextBtn">NEXT</button>
</div>`;
$('#container').html(incorrectFeedback);
}
}
});
}
function handleNextButton() {
$("#container").on('click', "#js-nextBtn", function() {
handleQuestionNum();
renderQuestion();
});
}
function handleRestartButton() {
$('#container').on('click', "#js-restartBtn", function() {
questionNum = 0;
score = 0;
renderQuestion();
handleStartButton();
});
}
function overallScore() {
const result = `
<div class="total-score">
<h2>You Scored ${score}/100!</h2>
<button type="submit" class="start-over" id="js-restartBtn">START OVER</button>
</div>`;
$('#container').html(result);
handleRestartButton();
}
function handleQuiz() {
handleStartButton();
handleSubmitButton();
handleNextButton();
}
handleQuiz();