-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
80 lines (68 loc) · 1.72 KB
/
main.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
window.addEventListener('load',init);
const levels = {
easy:5,
medium:3,
hard:2
}
const currentLevel = levels.medium;
let time = currentLevel;
let score = 0;
let isPlaying;
const wordInput = document.querySelector('#word-input');
const currentword = document.querySelector('#current-word');
const scoreDisplay = document.querySelector('#score');
const timeDisplay = document.querySelector('#time');
const message = document.querySelector('#message');
const seconds = document.querySelector('#seconds');
const words = ['test','revolver','echo','runway','joke','space','lucky','brick','play','hunt','beat'];
function init(){
seconds.innerHTML = currentLevel;
showWord(words);
wordInput.addEventListener('input',startMatch);
setInterval(countDown,1000);
setInterval(checkStatus,50);
}
function showWord(words){
const randIndex = Math.floor(Math.random() * words.length);
currentword.innerHTML = words[randIndex];
}
function countDown(){
if (time>0) {
time--;
}
else if(time===0){
isPlaying = false;
}
timeDisplay.innerHTML = time;
}
function checkStatus(){
if (!isPlaying && time===0) {
message.innerHTML = "Game Over!!";
score = -1;
}
}
function startMatch(){
if (matchWords()) {
isPlaying=true;
time=currentLevel + 1;
showWord(words);
wordInput.value ='';
score++;
}
if (score === -1) {
scoreDisplay.innerHTML = 0;
} else {
scoreDisplay.innerHTML = score;
}
}
function matchWords(){
if (wordInput.value===currentword.innerHTML) {
message.innerHTML='Correct!!';
return true;
}
else
{
message.innerHTML = '';
return false;
}
}