-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (126 loc) · 3.38 KB
/
index.js
File metadata and controls
145 lines (126 loc) · 3.38 KB
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
//create global variable for currnt qustion and increment
let currentQuestion = 0;
let score = 0;
let wrong = 0;
let pageCount = 0;
function startPage() {
$(".inner-container").html(`
<div class="btnBox">
<button tabindex="0" type="submit" class="startBtn">Start Quiz</button>
</div>
`);
}
function startQuiz() {
$('.score').text(`Correct: ${score}`);
$('.wrongScore').text(`Incorrect: ${wrong}`);
$('.startBtn').on('click', function() {
renderQuestion(currentQuestion);
});
}
function renderQuestion(num) {
pageCount++;
const quizQuestion = STORE[num].question;
const answerChoice = STORE[num].answers;
const answers = answerChoice.map(answer => (
`
<div class="col-6">
<div class="btnContainer">
<button role="button" tabindex="0" type="submit" class="js-answer_btn">${answer}</button>
</div>
</div>
`
)).join('');
$('.inner-container').html(
`
<form role="form">
<p class="center_Text">Question ${pageCount} of 10</p>
<h3 class="cur_quest" >${quizQuestion}</h3>
<fieldset>
<legend class="center_Text">Select an answer:</legend>
${answers}
</fieldset>
</form> `
);
}
//Render a new question after clicking next question
function nextQuestion() {
$('.inner-container').on("click", ".nextQuestion", function() {
currentQuestion++
if (currentQuestion <= 9) {
renderQuestion(currentQuestion);
} else {
renderFinalScore();
}
});
}
function getAnswer() {
$('.inner-container').on('click','.js-answer_btn', function(event) {
event.preventDefault()
let answerClicked = $(this).text();
confirmAnswer(answerClicked);
});
}
function confirmAnswer(answer) {
const CHECKANS = STORE[currentQuestion].rightAnswer;
if (answer == CHECKANS) {
score += 1;
$('.score').text(`Correct: ${score}`);
correctAnswer();
} else {
wrong += 1;
$('.wrongScore').text(`Incorrect: ${wrong}`);
wrongAnswer();
}
}
function correctAnswer() {
if (pageCount <= 9) {
$('.inner-container').html(
`<h1 class="check_answer">Correct!</h1>
<button type="submit" class="nextQuestion">Next Question</button>
`);
} else {
$('.inner-container').html(
`<h1 class="check_answer">Correct!</h1>
<button type="submit" class="nextQuestion">
Final Results
</button>
`);
}
}
function wrongAnswer() {
let isAnswer = STORE[currentQuestion].rightAnswer;
$('.inner-container').html(
` <h1 class="check_answer" >Incorrect!</h1>
<p class="corrected">The correct answer is ${isAnswer}.</p>
<button type="submit" class="nextQuestion">
Next Question
</button>
`);
}
function restartQuiz() {
$('.inner-container').on('click', '.restart', function() {
currentQuestion = 0;
score = 0;
wrong = 0;
pageCount = 0;
$('.score').text(`Correct: ${score}`);
$('.wrongScore').text(`Incorrect: ${wrong}`);
renderQuestion(currentQuestion);
});
}
function renderFinalScore() {
$('.inner-container').html(
`
<h3>Your final score is: ${score} / 10, or ${score * 10}% </h3>
<button type="submit" class="restart">Restart Quiz</button> `
);
}
//in charge of all functions that create event listeners
function handleQuizElements() {
startPage();
startQuiz();
nextQuestion();
getAnswer();
restartQuiz();
}
$(handleQuizElements);