-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
128 lines (90 loc) · 3.06 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
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
// Displaying slider number
// Syncing value of slider and arrow buttons
const slider = document.querySelector('#slider');
const minus1 = document.querySelector('#minus1');
const plus1 = document.querySelector('#plus1');
const inputDisplay = document.querySelector('#inputDisplay');
slider.addEventListener('input', function(){
inputDisplay.textContent = slider.value;
});
minus1.addEventListener('click', function(){
slider.value--;
inputDisplay.textContent = slider.value;
});
plus1.addEventListener('click', function(){
slider.value++;
inputDisplay.textContent = slider.value;
});
// generating random number
let randomNumber = Math.floor(Math.random()*100) + 1;
// number input
/* The following doesnt work because submit event fires upon clicking any buttons in form and we have two buttons (<>) which shouldnt submit
const form = document.querySelector('#form');
form.addEventListener('submit', function(){
let input = Number(slider.value); // passing through Number constructor to make sure value is a number. type String --> Number here
checkCorrect(input);
});
*/
const submit = document.querySelector('#submit');
submit.focus();
submit.addEventListener('click', function(){
let input = Number(slider.value); // passing through Number constructor to make sure value is a number. type String --> Number here
checkCorrect(input);
});
// Selecting DOM elements for displaying respective result
const result = document.querySelector('#result span');
const lowOrHi = document.querySelector('#lowOrHi');
const prevGuesses = document.querySelector('#prevGuesses');
const livesDOM = document.querySelector('#lives span');
const modalBg = document.querySelector('#modal-bg');
const newGame = document.querySelector('#newgame');
let lives =10;
// checking if guess correct
function checkCorrect(input){
if(input === randomNumber){
result.textContent = 'You Win!';
setGameOver();
}
else{
if(lives == 1)
setGameOver();
else{
wrongGuess(input);
}
}
}
function setGameOver(){
slider.disabled = true;
minus1.disabled = true;
plus1.disabled = true;
result.textContent = randomNumber;
// create a modal for reset button
modalBg.classList.add('modal-on');
}
//resetting game
document.querySelector('#reset').addEventListener('click', resetGame);
function resetGame(){
modalBg.classList.remove('modal-on');
randomNumber = Math.floor(Math.random()*100) + 1;
lives = 10;
slider.value = 50;
livesDOM.textContent = 10;
inputDisplay.textContent = 50;
prevGuesses.textContent = '';
lowOrHi.textContent = 'Enter your Guess!';
slider.disabled = false;
minus1.disabled = false;
plus1.disabled = false;
submit.focus();
}
// wrongGuess
function wrongGuess(input){
lives--;
livesDOM.textContent = lives;
if(input>randomNumber)
lowOrHi.textContent = 'Go Low!!';
else
lowOrHi.textContent = 'Go High!!';
prevGuesses.textContent = prevGuesses.textContent + ' ' + input;
submit.focus();
}