-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
102 lines (92 loc) · 2.76 KB
/
script.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
let min = 1,max=100;
let guess = Math.floor(Math.random()*(max-min+1))+min;
//,Take all the event from the HTML
const form = document.querySelector('form');
const submit = document.querySelector('#submit');
const numberGuess = document.querySelector('#numberGuess');
const note = document.querySelector('.note');
const preGuess = document.querySelector('#preGuess');
const remGuess = document.querySelector('#remGuess');
const cpreGuess = document.querySelector('.preGuess');
const cremGuess = document.querySelector('.remGuess');
const result = document.querySelector('.result');
let remGUESS=8;
let playGame = true;
const reStart = document.createElement('i');
if(playGame){
submit.addEventListener('click',(e)=>{
e.preventDefault();
let GUESS = parseInt(numberGuess.value);
ValidateGuess(GUESS);
})
}
function ValidateGuess (GUESS){
if(isNaN(GUESS) || GUESS=='' || GUESS>=101 || GUESS<=0){
note.innerHTML='INVALID';
numberGuess.value='';
} else{
checkRemchance(remGUESS,GUESS);
}
}
function checkRemchance(remGUESS,GUESS){
if(remGUESS===0){
numberGuess.value='';
form.style.display='none';
cpreGuess.style.display='none';
cremGuess.style.display='none';
let text =`You lose the game!! 😕 NUMBER WAS ${guess}`;
note.innerHTML=text;
endGame();
}else{
numberGuess.value='';
note.innerHTML='';
CheckGuess(GUESS);
updateRemGuess();
}
}
function CheckGuess (GUESS){
if(GUESS===guess){
form.style.display='none';
let text =`You win the game!! 🏆 NUMBER WAS ${guess}`;
note.innerHTML=text;
endGame();
newGame();
}else{
if(GUESS>guess){
let text = `Your GUESS 🛫 is too BIG!! 🐤`;
note.innerHTML=text;
}else if(GUESS<guess){
let text = `Your GUESS 🛬 is too SMALL 🐣`;
note.innerHTML=text;
}
updatePreGuess(GUESS);
}
}
function updatePreGuess (GUESS){
preGuess.innerHTML +=`${GUESS} `;
}
function updateRemGuess (){
remGUESS--;
remGuess.innerHTML=remGUESS;
}
function endGame(){
playGame=false;
reStart.classList.add('reStart');
reStart.innerHTML=`TRY AGAIN`;
result.appendChild(reStart);
newGame();
}
function newGame(){
reStart.addEventListener('click',function (e){
guess = Math.floor(Math.random()*(max-min+1))+min;
remGUESS=8;
playGame=true;
reStart.remove();
form.style.display='flex';
preGuess.innerHTML='';
remGuess.innerHTML=remGUESS;
cpreGuess.style.display='flex';
cremGuess.style.display='block';
note.innerHTML='';
})
}