-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
164 lines (141 loc) · 4.97 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
let questionNumber = 0;
let score = 0 ;
//---------listen for click to start quiz, then hide 'js-start-quiz' section on click & unhide 'js-quiz-form'-------------
function startQuiz() {
$('#js-q-number').text(questionNumber);
$('#js-score').text(score);
$('.js-start-button').click(function(){
$('.js-start-quiz').remove();
$('.js-quiz-section').css('display','block');
$('#js-q-number').text(questionNumber+1);
});
}
//---------Render html for final results----------
function renderResults() {
function resultMsg() {
if (score < 5) {return 'Yikes - You better stick to the beaches for now...'}
else if (score <8) {return 'Second mate status - not too shabby!'}
else {return 'Way to go, Captain! You know your stuff!'};}
$('.stats').hide();
$('.js-quiz-section').html(`
<div class="feedback">
<h1>Final Score:</h1>
<h1>${score}/10 Correct</h1>
<h2>${resultMsg()}</h2>
<button type="button" class="js-restart-button restart-button">Retake Quiz</button>
</div>`
);
}
//--------listen for click on restart quiz button-------
function restartQuiz() {
$('.js-quiz-section').on('click', '.js-restart-button', function(){
questionNumber = 0;
score = 0;
$('.stats').show();
takeQuiz();
$('#js-q-number').text(questionNumber+1);
});
}
//---------generate html for quiz questions-----------
function generateQuestion(){
console.log('generateQuestion called');
if (questionNumber < STORE.length) {
return `<form id="js-quiz-form">
<legend class="question">${STORE[questionNumber].question}</legend>
<fieldset class="radio-group">
<label class="option row1">
<input type="radio" value="${STORE[questionNumber].answers[0]}" name="answer" required><span>${STORE[questionNumber].answers[0]}</span>
</label>
<label class="option row2">
<input type="radio" value="${STORE[questionNumber].answers[1]}" name="answer" required><span>${STORE[questionNumber].answers[1]}</span>
</label>
<label class="option row3">
<input type="radio" value="${STORE[questionNumber].answers[2]}" name="answer" required><span>${STORE[questionNumber].answers[2]}</span>
</label>
<label class="option row4">
<input type="radio" value="${STORE[questionNumber].answers[3]}" name="answer" required><span>${STORE[questionNumber].answers[3]}</span>
</label>
<button type="submit" class="submit-button">Submit</button>
</fieldset>
</form>`;
}
else {
renderResults();
restartQuiz();
}
}
//Render to DOM html for quiz questions--------
function renderQuestion() {
$('.js-quiz-section').html(generateQuestion());
}
//--------handle user answer & go to next question------
function answeredCorrectly() {
score ++;
$('#js-score').text(score);
$('.js-quiz-section').html(`
<div class="feedback">
<img src="./photos-icons/firework-celebration.svg" alt="fireworks-celebration-icon">
<h2>Correct!</h2>
<p>${STORE[questionNumber].correctAnswerMsg}</p>
<button type="button" class="next-button js-next-button">Next</button>
</div>`
);
}
function answeredIncorrectly() {
$('.js-quiz-section').html(`
<div class="feedback">
<img src="./photos-icons/incorrect-icon.svg" alt="incorrect \'x\' icon">
<h2>Nope, that wasn't it</h2>
<p>The correct answer is: "${STORE[questionNumber].correctAnswer}"</p>
<p>${STORE[questionNumber].correctAnswerMsg}</p>
<button type="button" class="next-button js-next-button">Next</button>
</div>`
);
}
function goToNextQuestion(){
$('.js-quiz-section').on('click', '.js-next-button', function(){
incrementQuestion();
renderQuestion();
$('#js-q-number').text(questionNumber+1)
handleUserAnswers();
checkedLabelColor();
});
}
function handleUserAnswers(){
$('form').submit(function(event) {
console.log('handleUserAnswers called');
event.preventDefault();
let selected = $('input:checked');
let answer = selected.val();
let correctAnswer = `${STORE[questionNumber].correctAnswer}`
if (answer === correctAnswer) {
selected.parent().addClass('correct');
answeredCorrectly();
} else {
selected.parent().addClass('wrong');
answeredIncorrectly();
}
});
}
function incrementQuestion() {
questionNumber ++;
}
function updateScore() {
console.log('updateScore called');
}
//------Call all the functions to take the quiz
function takeQuiz(){
startQuiz();
renderQuestion();
handleUserAnswers();
goToNextQuestion();
}
$(takeQuiz);
//jQuery for stylesheet
function checkedLabelColor (){
$('form').on('change', 'input', function() {
$("label").removeClass("highlight");
if ($(this).is(":checked")) $(this).closest("label").addClass("highlight");
});
}
$(checkedLabelColor);