-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (131 loc) · 5.09 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
let questionNumber = 0;
let score = 0;
//generate question html
function generateQuestion () {
if (questionNumber < STORE.length) {
return `<div class="question-${questionNumber}">
<h2>${STORE[questionNumber].question}</h2>
<form>
<fieldset>
<label class="answerOption">
<input type="radio" value="${STORE[questionNumber].answers[0]}" name="answer" required>
<span>${STORE[questionNumber].answers[0]}</span>
</label>
<label class="answerOption">
<input type="radio" value="${STORE[questionNumber].answers[1]}" name="answer" required>
<span>${STORE[questionNumber].answers[1]}</span>
</label>
<label class="answerOption">
<input type="radio" value="${STORE[questionNumber].answers[2]}" name="answer" required>
<span>${STORE[questionNumber].answers[2]}</span>
</label>
<label class="answerOption">
<input type="radio" value="${STORE[questionNumber].answers[3]}" name="answer" required>
<span>${STORE[questionNumber].answers[3]}</span>
</label>
<button type="submit" class="submitButton">Submit</button>
</fieldset>
</form>
</div>`;
} else {
renderResults();
restartQuiz();
$('.questionNumber').text(10)
}
}
//increment question number
function changeQuestionNumber () {
//if (questionNumber < STORE.length) {
questionNumber ++;
//}
$('.questionNumber').text(questionNumber+1);
}
//increment score
function changeScore () {
score ++;
}
//start quiz
//on startQuizButton click hide start div
//unhide quiz form div
function startQuiz () {
$('.quizStart').on('click', '.startButton', function (event) {
$('.quizStart').remove();
$('.questionAnswerForm').css('display', 'block');
$('.questionNumber').text(1);
});
}
// render question in DOM
function renderQuestion () {
$('.questionAnswerForm').html(generateQuestion());
}
//user selects answer on submit run user feedback
function userSelectAnswer () {
$('form').on('submit', function (event) {
event.preventDefault();
let selected = $('input:checked');
let answer = selected.val();
let correctAnswer = `${STORE[questionNumber].correctAnswer}`;
if (answer === correctAnswer) {
selected.parent().addClass('correct');
ifAnswerIsCorrect();
} else {
selected.parent().addClass('wrong');
ifAnswerIsWrong();
}
});
}
function ifAnswerIsCorrect () {
userAnswerFeedbackCorrect();
updateScore();
}
function ifAnswerIsWrong () {
userAnswerFeedbackWrong();
}
//user feedback for correct answer
function userAnswerFeedbackCorrect () {
let correctAnswer = `${STORE[questionNumber].correctAnswer}`;
$('.questionAnswerForm').html(`<div class="correctFeedback"><div class="icon"><img src="${STORE[questionNumber].icon}" alt="${STORE[questionNumber].alt}"/></div><p><b>You got it right!</b></p><button type=button class="nextButton">Next</button></div>`);
}
//user feedback for wrong answer
function userAnswerFeedbackWrong () {
let correctAnswer = `${STORE[questionNumber].correctAnswer}`;
// let iconImage = `${STORE[questionNumber].icon}`;
$('.questionAnswerForm').html(`<div class="correctFeedback"><div class="icon"><img src="${STORE[questionNumber].icon}" alt="${STORE[questionNumber].alt}"/></div><p><b>You got it wrong</b><br>the correct answer is <span>"${correctAnswer}"</span></p><button type=button class="nextButton">Next</button></div>`);
}
//update score text
function updateScore () {
changeScore();
$('.score').text(score);
}
//when quiz is over this is the html for the page
function renderResults () {
if (score >= 8) {
$('.questionAnswerForm').html(`<div class="results correctFeedback"><h3>You're on fire!</h3><img src="https://i.imgur.com/OPODnDn.gif" alt="campfire animated icon"/><p>You got ${score} / 10</p><p>You're ready to plan your backpacking trip!</p><button class="restartButton">Restart Quiz</button></div>`);
} else if (score < 8 && score >= 5) {
$('.questionAnswerForm').html(`<div class="results correctFeedback"><h3>Almost there!</h3><img src="https://i.imgur.com/41Jnnxk.gif" alt="raccoona animated icon"/><p>You got ${score} / 10</p><p>Bone up on your backpacking knowledge a little more and you'll be ready to go!</p><button class="restartButton">Restart Quiz</button></div>`);
} else {
$('.questionAnswerForm').html(`<div class="results correctFeedback"><h3>You might want to stick with car camping</h3><img src="https://i.imgur.com/iGqoVcM.gif" alt="car animcated icon"/><p>You got ${score} / 10</p><p>With more camping and outdoor experience you'll be able to pass this quiz in no time</p><button class="restartButton">Restart Quiz</button></div>`);
}
}
//what happens when the user clicks next
function renderNextQuestion () {
$('main').on('click', '.nextButton', function (event) {
changeQuestionNumber();
renderQuestion();
userSelectAnswer();
});
}
//restart quiz function - reloads page to start quiz over
function restartQuiz () {
$('main').on('click', '.restartButton', function (event) {
location.reload();
});
}
//run quiz functions
function createQuiz () {
startQuiz();
renderQuestion();
userSelectAnswer();
renderNextQuestion();
}
$(createQuiz);