-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (56 loc) · 2.38 KB
/
script.js
File metadata and controls
72 lines (56 loc) · 2.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
const quotes = [
'Things are only impossible until they are not',
'It is possible to commit no errors and still lose. That is not a weakness. That is life',
'There is a way out of every box, a solution to every puzzle; it is just a matter of finding it.',
'Without freedom of choice there is no creativity',
'Logic is the beginning of wisdom, not the end',
'Improve a mechanical device and you may double productivity. But improve yourself, you gain a thousandfold',
'Compassion: that is the one thing no machine ever had. Maybe it is the one thing that keeps us ahead of them.',
];
const quote = document.getElementById('quote');
const input = document.getElementById('typed-value');
const start = document.getElementById('start');
const message = document.getElementById('message');
let wordQueue;
let highlightPosition;
let startTime;
function startGame() {
console.log("Game started!");
const quoteIndex = Math.floor(Math.random() * quotes.length);
const quoteText = quotes[quoteIndex];
wordQueue = quoteText.split(' ');
quote.innerHTML = wordQueue.map(word => (`<span>${word}</span>`)).join('');
highlightPosition = 0;
quote.childNodes[highlightPosition].className = 'highlight';
input.focus();
input.value = '';
message.innerText = '';
startTime = new Date().getTime();
document.body.className = "";
start.className = "started";
setTimeout(() => { start.className = "button"; }, 2000);
}
function checkInput() {
const currentWord = wordQueue[0].replaceAll(".", "").replaceAll(",", "");
const typedValue = input.value.trim();
if (currentWord !== typedValue) {
input.className = currentWord.startsWith(typedValue) ? "" : "error";
return;
}
wordQueue.shift();
input.value = "";
quote.childNodes[highlightPosition].className = "";
if (wordQueue.length === 0) {
gameOver();
return;
}
highlightPosition++;
quote.childNodes[highlightPosition].className = 'highlight';
}
function gameOver() {
const elapsedTime = new Date().getTime() - startTime;
document.body.className = "winner";
message.innerHTML = `<span class="congrats">Congratulations!</span> <br> You finished in ${elapsedTime / 1000} seconds.`;
}
start.addEventListener('click', startGame);
input.addEventListener('input', checkInput);