-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
135 lines (119 loc) · 3.15 KB
/
script.js
File metadata and controls
135 lines (119 loc) · 3.15 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
//var definitions
var timer = document.getElementById("timer");
var currentTime = 60;
var startBtn = document.getElementById("start");
var titleRem = document.getElementById("Title");
var quizQ = document.getElementById("quiz");
var questionPrompt = document.getElementById("question-prompt");
var choices = document.getElementById("choices");
var scores = document.getElementById("ScoresHead");
var index = 0;
var timerDown;
var saved = [];
var questions = [
{
question: "Which of the following is NOT a primitive value?",
options: ["Null", "Boolean", "String", "Sound"],
correct: "Sound",
},
{
question: "What element of HTML is not seen by the user?",
options: ["<body>", "<img>", "<head>", "<div>"],
correct: "<head>",
},
{
question: "A function can be ended with the statement:",
options: ["return", "end", "stop", "conclude"],
correct: "return",
},
{
question: "Which of the following is a common CSS term?",
options: ["control", "padding", "commit", "function"],
correct: "padding",
},
];
//Function Logic
function Countdown() {
if (currentTime > 0) {
currentTime--;
timer.textContent = currentTime;
}
if (currentTime <= 0) {
currentTime = 0;
endGame();
}
}
function quizStart() {
titleRem.setAttribute("class", "hidden");
quizQ.removeAttribute("class", "hidden");
nextQ();
timerDown = setInterval(Countdown, 1000);
Countdown();
}
function nextQ() {
questionPrompt.textContent = questions[index].question;
for (var i = 0; i < questions[index].options.length; i++) {
var cycle = document.createElement("button");
var li = document.createElement("li");
cycle.textContent = questions[index].options[i];
li.appendChild(cycle);
choices.append(li);
}
}
function questionA(event) {
var pressed = event.target;
if (!pressed.matches("button")) {
return;
}
if (pressed.textContent !== questions[index].correct) {
currentTime -= 10;
timer.textContent = currentTime;
} else {
index++;
if (index <= 3) {
questionPrompt.innerHTML = "";
choices.innerHTML = "";
nextQ();
} else {
endGame();
}
}
}
function endGame() {
var highscore = document.createElement("li");
var name = prompt("Please enter your name");
var input = name.concat(": ").concat(currentTime);
highscore.append(input);
scores.append(highscore);
saved.push(input);
quizQ.setAttribute("class", "hidden");
titleRem.setAttribute("class", "Title");
index = 0;
questionPrompt.innerHTML = "";
choices.innerHTML = "";
clearInterval(timerDown);
currentTime = 60;
console.log(saved);
saving();
}
function saving() {
var arrayS = JSON.stringify(saved);
localStorage.setItem("scores", arrayS);
console.log(saved.length);
}
function loading() {
var load = localStorage.getItem("scores");
var reload = JSON.parse(load);
for (var i = 0; i < reload.length; i++) {
var loaditems = document.createElement("li");
var pre = reload[i];
loaditems.append(pre);
scores.append(loaditems);
}
}
//Additional Logic
startBtn.onclick = quizStart;
choices.onclick = questionA;
if (localStorage.getItem("scores")){
loading();
}