-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (155 loc) · 5.58 KB
/
script.js
File metadata and controls
163 lines (155 loc) · 5.58 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// -get score, enter initials, save to LS
//variables
const SCORE_BOARD_KEY = "scoreBoard";
var score = 0;
var timer = 0;
var timerInterval;
var currentQuestionnaireIndex = 0;
var submitScore = document.querySelector("#submitScore");
var rightOrWrongEl = document.querySelector("#right-or-wrong");
var timerEl = document.querySelector("#timer");
var startButton = document.querySelector(".start-button");
var header = document.querySelector("header");
var main = document.querySelector("main");
var questionBox = document.querySelector("#questions-container");
var choicesBox = document.querySelector("#choices-container");
var sectionEl = document.querySelector("section");
var questionnaires = [
{
question: "Which of the following would you use to for in-line comments?",
answers: [
{ text: "//", correct: true },
{ text: "--", correct: false },
{ text: "[]", correct: false },
],
},
{
question: "What is an index?",
answers: [
{
text: "A special variable, which can hold more than one value at a time",
correct: true
},
{
text: "A special character, which can hold one value",
correct: false
},
{ text: "A symbol", correct: false },
],
},
{
question: "What is a syntax?",
answers: [
{ text: "A number type that define values", correct: false },
{ text: "A value", correct: false },
{ text: "A defines two types of values: Fixed values and variable values", correct: true },
],
},
{
question: "What are variables?",
answers: [
{ text: "Containers for storing html values", correct: false },
{ text: "Containers for storing data values", correct: true },
{ text: "Containers for storing css values", correct: false },
],
},
{
question: "What are strings?",
answers: [
{ text: "Are used for storing and manipulating numbers", correct: false },
{
text: "Are used for storing and manipulating text",
correct: true
},
{
text: "Are used for storing and manipulating integers",
correct: false
},
],
},
];
//question loop
//attach this to the click handler for the start button
function startQuiz() {
timer = 80;
function myTimer() {
timer--;
timerEl.textContent = timer;
if (timer <= 0) {
endGame();
}
}
timerInterval = setInterval(myTimer, 1000);
displayQuestionnaire();
}
function displayQuestionnaire() {
var questionnaire = questionnaires[currentQuestionnaireIndex];
questionBox.innerHTML = questionnaire.question;
var answers = questionnaire.answers;
choicesBox.innerHTML = "";
for (var answer of answers) {
var br = document.createElement("br");
var buttonEl = document.createElement("button");
buttonEl.classList.add("btn", "btn-secondary");
buttonEl.innerText = answer.text;
buttonEl.value = answer.correct;
choicesBox.appendChild(buttonEl);
buttonEl.addEventListener("click", function () {
var correct = this.value;
if (correct === "true") {
rightOrWrongEl.innerText = "That's correct 😻";
score++;
} else {
rightOrWrongEl.innerText = "WRONG!";
timer -= 5;
}
currentQuestionnaireIndex++;
if (currentQuestionnaireIndex < questionnaires.length) {
displayQuestionnaire();
} else {
endGame();
}
});
}
}
function endGame() {
clearInterval(timerInterval);
timerEl.style.display = "none";
questionBox.style.display = "none";
choicesBox.style.display = "none";
rightOrWrongEl.style.display = "none";
submitScore.style.display = "block";
var scoreEl = submitScore.querySelector("#score");
scoreEl.innerText = "Your Score is: " + score;
var highestScore = submitScore.querySelector("#highestScore");
var scoreButton = submitScore.querySelector("#scoreButton");
var nameInput = submitScore.querySelector("#playerName");
scoreButton.addEventListener("click", function () {
var scoreBoardEntry = { userName: nameInput.value, score: score };
var scoreBoardEntries = localStorage.getItem(SCORE_BOARD_KEY);
if (scoreBoardEntries) {
scoreBoardEntries = JSON.parse(scoreBoardEntries);
} else {
scoreBoardEntries = [];
}
scoreBoardEntries.push(scoreBoardEntry);
scoreBoardEntries.sort((e1, e2) => e2.score - e1.score);
localStorage.setItem(SCORE_BOARD_KEY, JSON.stringify(scoreBoardEntries));
for (var highScoreEntry of scoreBoardEntries) {
var pTag = document.createElement("p");
pTag.innerText = "Player Entry: " + highScoreEntry.userName + " score: " + highScoreEntry.score;
highestScore.appendChild(pTag);
}
});
}
var clearHighscoreBtn = submitScore.querySelector("#clearHighscoreBtn");
clearHighscoreBtn.addEventListener("click", function () {
localStorage.removeItem(SCORE_BOARD_KEY);
highestScore.innerHTML = "";
});
// when user clicks start quiz button, clear out existing text in the main and header tag.
startButton.addEventListener("click", function () {
main.style.display = "none";
header.style.display = "none";
startQuiz();
});