-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (92 loc) · 3.69 KB
/
script.js
File metadata and controls
108 lines (92 loc) · 3.69 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
const word = document.getElementById('word');
const text = document.getElementById('text');
const settingsForm = document.getElementById('settings-form');
const selectDifficulty = document.getElementById('difficulty');
const scoreElement = document.getElementById('score');
const timeElement = document.getElementById('time');
const endGame = document.getElementById('end-game-container');
const settings = document.getElementById('settings');
const words = [
'admit','academic','access','acid','abandoned','airplane','angular',
'builds','bunch','butter','buzz','bad','ball',
'cadillac','calculated','cameroon','capture','conventional',
'destination','detection','dialog','disclosure','drag','dependent','delhi',
'ecommerce','eight','english','entrepreneur','essay','europe',
'fabric','favorite','fellowship','flickr','folder','freeware','feeble',
'gauge','gazette','geographic','glossary','guatemala',
'hammer','hawaiian','highlights','hydrogen','hurricane','highfalutin',
'illinois','impossible','independence','indie','issues',
'javascript','justify','journal','joining','judgment','juice','jaipur',
'karma','kernel','kilometers','kitty','keyboard','kanpur',
'lafayette','labels','legend','liability','losses','loving',
'madagascar','mainstream','memorabilia','messages','miscellaneous','mumbai',
'navigation','necessary','neutral','newsletter','numerous','north','node',
'occasions','officially','offset','omissions','orchestra',
'pages','parameter','participants','permission','podcast','patna','pies',
'qualification','queue','quizzes','quotes','quick',
'roulette','raleigh','reader','realm','recognition','recommendation','referral','rocks','react','redux',
'satellite','scanner','scholarship','screenshot','seems','stack','simplified','soccer','sophisticated','steer','silver','superficial','sigh',
'taught','technology','tense','terrain','titles','transparency','typically',
'ultimately','undefined','usually','unwrap','university','unity',
'venue','verify','version','viewed','volleyball',
'waiver','walked','warranty','website','workplace','warlike',
'yarn','yugoslavia','yeah','yearly','yard',
'zero','zone','zoom','zimbabwe',
];
let randomeWord;
let score = 0;
let time = 10;
let difficulty = localStorage.getItem('difficulty') !== null ? localStorage.getItem('difficulty') : 'medium';
selectDifficulty.value = localStorage.getItem('difficulty') !== null ? localStorage.getItem('difficulty') : 'medium';
text.focus();
const timeInterval = setInterval(updateTime, 1000);
function updateTime() {
time--;
timeElement.innerHTML = time + 's';
if (time === 0) {
clearInterval(timeInterval)
gameOver();
}
}
function gameOver() {
endGame.innerHTML = `
<h1>Time Over!</h1>
<p>Your final score is ${score}</p>
<button onclick="location.reload()" style="width: 120px; border-radius: 5px; height: 50px; font-weight: bolder;">Reload</button>
`
endGame.style.display = 'flex';
}
function getRandomWord() {
return words[Math.floor(Math.random() * words.length)]
}
function addWordToDom() {
randomeWord = getRandomWord();
word.innerHTML = randomeWord;
}
function updateScore() {
score++;
scoreElement.innerHTML = score;
}
addWordToDom();
text.addEventListener('input', e => {
const insertedText = e.target.value;
if(insertedText === randomeWord) {
addWordToDom();
updateScore();
e.target.value = '';
if(difficulty === 'hard') {
time += 2;
} else if (difficulty === 'medium') {
time += 3;
} else if (difficulty === 'easy') {
time += 4;
} else {
time += 5;
}
updateTime();
}
})
settingsForm.addEventListener('change', e => {
difficulty = e.target.value;
localStorage.setItem('difficulty', difficulty);
})