-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
182 lines (112 loc) · 6.21 KB
/
javascript.js
File metadata and controls
182 lines (112 loc) · 6.21 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
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
const minnum = document.querySelector('.minnumber'),
maxnum = document.querySelector('.maxnumber'),
getinput = document.querySelector('#guessnumber'),
getbtn = document.querySelector('#btn'),
message1 = document.querySelector('.message1'),
message2 = document.querySelector('.message2');
getgame = document.querySelector('#game');
let min = 1,
max = 10,
gameLeft = 3,
winningnum = randomnum(min,max);
minnum.textContent = min;
maxnum.innerText = max;
getbtn.addEventListener('click', function(){
// console.log("working");
// console.log(getinput.value);
// console.log(typeof getinput.value);
// let guess = Number(getinput.value);
// let guess = +getinput.value;
let guess = parseInt(getinput.value);
// console.log(guess);
// console.log(typeof guess);
if(guess < min || guess > max || isNaN()){
// console.log(`Please enter a number between ${min} to ${max}`);
// message2.textContent = `Please enter a number between ${min} to ${max}`;
setmessage2(`Please enter a number between ${min} to ${max}`,"red");
}
if(guess === winningnum) {
// game WON
// disable getinput
// getinput.disabled = true;
// getinput border color to green
// getinput.style.borderColor = "green";
// message1
// message1.textContent = `${winningnum} is correct!!!, You Won`;
// message1.style.color = "green";
// setmessage1(`${winningnum} is correct!!!, You Won`,"green");
// play again ?
// getbtn.value = 'Play Again';
gameover(true,`${winningnum} is correct!!!, You Won`);
}else{
// Wrong number
// gameLeft--;
gameLeft -= 1;
// console.log(gameLeft);
if(gameLeft === 0) {
// game over LOSE
// disabled getinput
// getinput.disabled = true;
// getinput border to red
// getinput.style.borderColor = "red";
// message1
// message1.textContent = `Game Over, You Lost, The corrct number is ${winningnum}`;
// message1.style.color = "red";
// setmessage1(`Game Over, You Lost, The corrct number is ${winningnum}`,"red");
// play again ?
// getbtn.value = "Play Again";
gameover(false,`Game Over, You Lost, The corrct number is ${winningnum}`);
}else{
// continue Game
// getinput border color to red
getinput.style.borderColor = "red";
// clear getinput old value
getinput.value = "";
// message1
// message1.innerText = `${guess} is not correct , ${gameLeft} guess left`;
// message1.style.color = "blue";
setmessage1(`${guess} is not correct , ${gameLeft} guess left`, "blue");
}
}
});
//Message
function setmessage1(msg,color){
message1.textContent = msg;
message1.style.color = color;
}
function setmessage2(msg,color){
message2.textContent = msg;
message2.style = color;
setTimeout(function(){
message2.innerText = "";
},2000);
}
// Game over
function gameover(won,msg){
let color;
won === true ? color = "green" : color = "red";
//getinput disabled
getinput.disabled = true;
// getinput border color to green or red
getinput.style.borderColor = color;
// message1
setmessage1(msg,color);
// play again ?
getbtn.value = 'Play Again';
// add class name
// getbtn.className = "btn playagain";
getbtn.classList.add('playagain');
}
getgame.addEventListener('mouseup',function(e){
console.log(e.target);
if(e.target.className === "btn playagain"){
// console.log("working");
window.location.reload();
}
});
// For Winning Number
function randomnum(min,max){
// 10-1 + 1
let getrdm = Math.floor(Math.random()*(max-min)+1);
return getrdm;
}