-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessnumber.js
71 lines (58 loc) · 1.77 KB
/
Guessnumber.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
// Fisrt of all let the pc hold a number
const randomNumber=Math.ceil(Math.random()*20)
const again=document.querySelector(".again")
const guess=document.querySelector(".guess")
const check=document.querySelector(".check")
const message=document.querySelector(".msg")
const body=document.querySelector("body")
const life=document.querySelector(".life")
console.log(randomNumber)
let hak=10;
let topScore=0
//! 2- When I was pressed on Check button :
//check.onClick=()=>{}
check.addEventListener("click",()=>{
const tahmin =guess.value;
//? If you dont enter nay value, avoid to click on check
if(!tahmin){
message.textContent="Kindly enter a guess "
}
else if(tahmin<0 && tahmin>20){
message.textContent="Enter a value between 0-20"
}
else if(tahmin==randomNumber){
message.textContent="Correct answer, Congrats!";
body.style.backgroundColor="green"
document.querySelector(".number").textContent=randomNumber
check.disabled=true
if(hak>topScore){
topScore=hak;
document.querySelector(".topScore").textContent=topScore
}
}
//! If the guessed number is wrong, add reduce / increased control
else{
if(hak>1){
hak--
life.textContent=hak
tahmin>randomNumber
? message.textContent="Hint👉:Reduce the number"
: message.textContent="Hint👉:Increase the number"
} else {
body.style.backgroundColor="red"
message.textContent=" Sorry, You lost the game 😥"
check.disabled=true
life.textContent=0
}
}
});
again.addEventListener("click",()=>{
const randomNumber = Math.ceil(Math.random()*18 + 1);
message.textContent="The game getting ready for the new player"
body.style.backgroundColor="blue";
document.querySelector(".number").textContent="?";
check.disabled=false;
life.textContent=10
hak=10
guess.value=""
})