-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
297 lines (254 loc) · 8.37 KB
/
index.html
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Times Tables Generator</title>
<style>
body {
font-family: Verdana;
}
#checkAnswerButton {
margin-bottom: 50px;
}
.title {
margin-bottom: 6px;
margin-top: 15px;
}
.header {
position: fixed;
padding-left: 20px;
top: 0;
right: 0;
left: 0;
z-index: 999;
height: 95px;
background: #fff;
}
.content {
padding-top: 65px;
}
#number {
display: inline-block;
width: 32px;
height: 30px;
text-align: right;
}
#question {
display: inline-block;
width: 90px;
padding-left: 10px;
text-align: left;
}
#answer {
width: 40px;
}
#backToMenu {
font-size: 12px;
padding-bottom: 8px;
padding-left: 15px;
display: inline-block;
}
</style>
</head>
<body>
Times Tables Generator
<template id="chooseLevelContainerTemplate">
<h2>Choose a Level</h2>
<div id="levelOptions"></div>
</template>
<template id="chooseLevelItem">
<a id="link" level="1" href="#" onClick="startLevel(this.attributes.level)"></a><br />
</template>
<template id="answerQuestionsTemplate">
<div id="header" class="header">
<h2 id="title" class="title">Level X</h2>
<div id="timer">[Timer goes here]</div>
<div id="finalAnswer"></div>
</div>
<div class="content">
<a href="#" id="backToMenu" onClick="goBackToMenu()">Go back to menu</a>
<form id="form">
<div id="questions"></div>
<button id="checkAnswerButton" type="button" onClick="checkAnswers()">Check Answers</button>
</form>
</div>
</template>
<template id="questionTemplate">
<div id="question_">
<span id="number"></span><span id="question"></span><input type="tel" id="answer" tabindex=1 enterkeyhint="next" /><span id="result"></span>
</div>
</template>
<div id="container">
</div>
<script type="text/javascript">
function generateLevelChoices() {
clearContainer()
var container = document.getElementById("container")
var chooseLevelContainer = document.getElementById("chooseLevelContainerTemplate").content.cloneNode(true)
var levelsGoHere = chooseLevelContainer.getElementById("levelOptions")
var chooseLevelItem = document.getElementById("chooseLevelItem").content
for( var i=1; i <= 11; i += 1) {
var element = chooseLevelItem.cloneNode(true)
element.getElementById("link").attributes.level = i
element.getElementById("link").innerText = "Level " + i // could also do `Level ${i}`
levelsGoHere.appendChild(element)
}
container.appendChild(chooseLevelContainer)
}
function clearContainer() {
var container = document.getElementById("container")
container.innerHTML = ""
}
function startLevel(level) {
console.log("start level " + level)
stopCountdownTimer()
clearContainer()
var options = optionsForLevel(level)
var tables = generateTimesTables(options)
displayTimesTables(tables)
}
function optionsForLevel(level) {
if (level == 1) {
return {name: "1", total: 10, timesTables:[2], highestMultiplier: 10, lowestMultiplier: 1}
} else if (level == 2) {
return {name: "2", total: 15, timesTables:[2, 10], highestMultiplier: 10, lowestMultiplier: 1}
} else if (level == 3) {
return {name: "3", total: 20, timesTables:[2, 5, 10], highestMultiplier: 10, lowestMultiplier: 1}
} else if (level == 4) {
return {name: "4", total: 25, timesTables:[2, 4, 5, 10], highestMultiplier: 10, lowestMultiplier: 1}
} else if (level == 5) {
return {name: "5", total: 30, timesTables:[2, 4, 5, 8, 10], highestMultiplier: 11, lowestMultiplier: 1}
} else if (level == 6) {
return {name: "6", total: 35, timesTables:[2, 3, 4, 5, 8, 10], highestMultiplier: 12, lowestMultiplier: 1}
} else if (level == 7) {
return {name: "7", total: 40, timesTables:[2, 3, 4, 5, 6, 8, 10], highestMultiplier: 12, lowestMultiplier: 1}
} else if (level == 8) {
return {name: "8", total: 45, timesTables:[2, 3, 4, 5, 6, 8, 9, 10], highestMultiplier: 12, lowestMultiplier: 1}
} else if (level == 9) {
return {name: "9", total: 50, timesTables:[2, 3, 4, 5, 6, 7, 8, 9, 10], highestMultiplier: 12, lowestMultiplier: 1}
} else if (level == 10) {
return {name: "10", total: 100, timesTables:[2, 3, 4, 5, 6, 7, 8, 9, 10], highestMultiplier: 12, lowestMultiplier: 1}
} else if (level == 11) {
return {name: "11", total: 60, timesTables:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11], highestMultiplier: 12, lowestMultiplier: 0}
}
// ÷
}
function generateTimesTables(options) {
var total = options.total
var timesTables = options.timesTables
var lowestMultiplier = options.lowestMultiplier
var highestMultiplier = options.highestMultiplier
var timesTablesData = {name: `Level ${options.name}`, questions:[]}
for(var i=0; i < total; i++) {
var table = randomItemIn(timesTables)
var multiplier = randomNumberUpTo(lowestMultiplier, highestMultiplier)
timesTablesData.questions.push({left: table, right: multiplier})
}
return timesTablesData
}
function displayTimesTables(timesTables) {
console.log(timesTables)
var container = document.getElementById("container")
var answerQuestionsContainer = document.getElementById("answerQuestionsTemplate").content.cloneNode(true)
var questionsGoHere = answerQuestionsContainer.getElementById("questions")
var question = document.getElementById("questionTemplate").content
for(var i=0; i < timesTables.questions.length; i++) {
var sum = timesTables.questions[i]
// create something so we can see the sum
var element = question.cloneNode(true)
element.getElementById("question_").id = "question_" + i
element.getElementById("number").innerText = `${i+1}.`
element.getElementById("question").innerText = `${sum.left} x ${sum.right} =`
element.getElementById("answer").tabIndex = (i+1)
// add the sum to the container
questionsGoHere.appendChild(element)
}
answerQuestionsContainer.querySelector("#title").innerText = timesTables.name
container.appendChild(answerQuestionsContainer)
window.timesTables = timesTables
startCountdownTimer()
}
function checkAnswers() {
stopCountdownTimer()
var timesTables = window.timesTables.questions
var totalCorrectAnswers = 0
for(var i=0; i<timesTables.length; i++) {
var question = timesTables[i]
var correctAnswer = question.left * question.right
var questionNode = document.getElementById("question_"+i)
var answerNode = questionNode.querySelector("#answer")
answerNode.disabled = true
var answer = answerNode.value
if (answer != "" && answer == correctAnswer) {
questionNode.querySelector("#result").innerText = "✅"
totalCorrectAnswers += 1
} else {
questionNode.querySelector("#result").innerText = "❌"
}
}
document.querySelector("#checkAnswerButton").style.display = "none"
document.querySelector("#finalAnswer").innerText = `You scored ${totalCorrectAnswers} out of ${timesTables.length}`
}
function stopTimesTables() {
stopCountdownTimer()
window.onscroll = null
}
function goBackToMenu() {
stopTimesTables()
generateLevelChoices()
}
function randomItemIn(items) {
var item = items[Math.floor(Math.random() * items.length)]
return item
}
// BUG:
function randomNumberUpTo(lowest, highest) {
let diff = highest - lowest
return lowest + Math.round(diff * Math.random())
}
function startCountdownTimer() {
// get the end time (aka now + 3 minutes)
let endtime = Date.now() + (1000 * 60 * 3)
document.querySelector("#timer").innerText = "GO!"
// update the timer straight away!
let timeRemaining = getTimeRemaining(endtime)
document.querySelector("#timer").innerText = `${timeRemaining.minutes}m ${timeRemaining.seconds}s`
document.timeInterval = setInterval(function(){
let timeRemaining = getTimeRemaining(endtime)
// update something on screen
document.querySelector("#timer").innerText = `${timeRemaining.minutes}m ${timeRemaining.seconds}s`
// check if there is no time left, and STOP THE TEST
if (timeRemaining.total <= 0) {
document.querySelector("#timer").innerText = `Time's up!`
checkAnswers()
stopCountdownTimer()
}
}, 1000)
}
function stopCountdownTimer() {
clearInterval(document.timeInterval)
document.timeInterval = null
}
function getTimeRemaining(endtime){
var t = endtime - Date.now()
var seconds = Math.floor( (t/1000) % 60 )
var minutes = Math.floor( (t/1000/60) % 60 )
return {
'total': t,
'minutes': minutes,
'seconds': seconds
};
}
window.onload = function() {
generateLevelChoices()
/*
var options = optionsForLevel(2)
var tables = generateTimesTables(options)
displayTimesTables(tables)
*/
}
// level 1 -
</script>
</body>
</html>