-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
144 lines (139 loc) · 4.19 KB
/
script.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
// sB = startButton
const sB = document.getElementById("start-btn")
// nB = nextButton
const nB = document.getElementById("next-btn")
// hSB = highScoreButton
const hSB = document.getElementById("highScore-btn")
// hSB = highScoreButton
const save = document.getElementById("save-btn")
// qCE = questionContainerElement
const qCE = document.getElementById("questions-container")
// qE = questionElement
const qE = document.getElementById("questions")
// aBE = answerButtonsElement
const aBE = document.getElementById("answer-buttons")
// sQ = shuffledQuestions, cQI = currentQuestionIndex
let sQ, cQI
var base = document.getElementById("score");
// link to highscore.html
document.getElementById("highScore-btn").onclick = function(){
location.href = "highscores.html";
}
sB.addEventListener("click", startQuiz)
nB.addEventListener("click", () => {
cQI++
setNextQuestion()
})
function startQuiz() {
sB.classList.add("hide")
hSB.classList.add("hide")
sQ = questions.sort(() => Math.random() - .5)
cQI = 0
qCE.classList.remove("hide")
setNextQuestion()
}
function setNextQuestion() {
reset()
showQuestion(sQ[cQI])
}
function showQuestion(questions) {
qE.innerText = questions.question
questions.answers.forEach(answer => {
const button = document.createElement("button")
button.innerText = answer.text
button.classList.add("btn")
if (answer.correct) {
button.dataset.correct = answer.correct
}
button.addEventListener("click", selectAnswer)
aBE.appendChild(button)
})
}
function reset() {
clearStatusClass(document.body)
nB.classList.add("hide")
while (aBE.firstChild) {
aBE.removeChild(aBE.firstChild)
}
}
function selectAnswer(e) {
const selectedButton = e.target
const correct = selectedButton.dataset.correct
adjustScore(correct)
setStatusClass(document.body, correct)
Array.from(aBE.children).forEach(button => {
setStatusClass(button, button.dataset.correct)
})
if (sQ.length > cQI + 1) {
nB.classList.remove("hide")
} else {
sB.innerText = "Restart"
sB.classList.remove("hide")
save.classList.remove("hide")
hSB.classList.remove("hide")
}
}
function adjustScore (correct){
var score = base.innerHTML;
var add = 25;
if(correct){
points = score + add
console.log("yay")
} else{
console.error()
}
}
function setStatusClass(element, correct) {
clearStatusClass(element)
if (correct) {
element.classList.add("correct")
} else {
element.classList.add("wrong")
}
}
function clearStatusClass(element) {
element.classList.remove("correct")
element.classList.remove("wrong")
}
function saveScore(){
localStorage.setItem("name", base.val());
}
var stored = localStorage.getItem("name", base);
const questions = [
{
question: "when calling Javascript, what is the correct tag you would use?",
answers: [
{ text: "<div>", correct: false},
{ text: "<section>", correct: false},
{ text: "<script>", correct: true },
{ text: "<header>", correct: false}
]
},
{
question: "The condition in an if/else statement is enclosed within ______.",
answers: [
{ text: "quotes", correct: false},
{ text: "brackets", correct: false},
{ text: "parentheses", correct: true},
{ text: "square brackets", correct: false}
]
},
{
question: "Is JavaScript case-sensitive?",
answers: [
{ text: "Yes", correct: true},
{ text: "No", correct: false},
{ text: "Idk", correct: false},
{ text: "depends", correct: false}
]
},
{
question: "Which type of pop up, allows a user to type a response?",
answers: [
{ text: "input", correct: false},
{ text: "prompt", correct: true},
{ text: "alert", correct: false},
{ text: "confirm", correct: false}
]
}
]