-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorks.js
157 lines (96 loc) · 3.02 KB
/
Works.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
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
var score=0;
var life=3;
document.getElementById('playnow').addEventListener('click' , function(){
const val = document.getElementById('Shan');
const val2 = document.getElementById('play');
val.classList.add('hidden');
val.classList.remove('flex');
val2.classList.remove('hidden');
continueGame();
life=4;
});
function randomAlphabet(){
const string = "abcdefghijklmnopqrstuvwxyz";
const alphabets = string.split('');
const randomNumber = Math.random() * 25;
const index = Math.round(randomNumber);
const alpha = alphabets[index];
return alpha;
}
function continueGame(){
const alphabet = randomAlphabet();
const obj = document.getElementById('texter');
obj.innerText = alphabet;
setBGYellow(alphabet);
}
function setBGYellow(elementID){
const obj = document.getElementById(elementID);
obj.classList.add('bg-yellow-600');
}
function removeBG(elementID){
const obj = document.getElementById(elementID);
obj.classList.remove('bg-yellow-600');
}
function scoreAdder(){
score++;
const text = document.getElementById('scoreAble');
text.innerText=score;
}
function LifeRemover(){
life--;
const lifer = document.getElementById('life');
lifer.innerText=life;
}
function handleKeyboardPress(event){
const playerPressed = event.target.innerText;
const playerPressure = playerPressed.toLowerCase();
const currentAlphabet = document.getElementById('texter').innerText;
const currentAlphabet2 = currentAlphabet.toLowerCase();
if (playerPressure == currentAlphabet2){
removeBG(currentAlphabet2);
scoreAdder();
continueGame();
}
else if (playerPressure =="escape"){
gameOver();
}
else{
if (life==0){
gameOver();
life=3;
}
else LifeRemover();
}
}
function gameOver(){
const val = document.getElementById('play');
const val2 = document.getElementById('scorecard');
val.classList.add('hidden');
val2.classList.remove('hidden');
finalScoreAppender();
const currentAlphabet = getElementTextByID('texter');
removeBG(currentAlphabet);
score=0;
const elem = document.getElementById('scoreAble');
elem.innerText=score;
life=3;
const elem2 = document.getElementById('life');
elem2.innerText=life;
}
function finalScoreAppender(){
const element = document.getElementById('final-score');
element.innerText = score;
}
//the function maintaining it
document.addEventListener('click' ,handleKeyboardPress);
document.getElementById('playagain').addEventListener('click' , function(){
const val = document.getElementById('Shan');
const val2 = document.getElementById('scorecard');
val.classList.remove('hidden');
val2.classList.add('hidden');
});
function getElementTextByID(elementId){
const element = document.getElementById(elementId);
const text = element.innerText;
return text;
}