-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (47 loc) · 1.64 KB
/
Copy pathscript.js
File metadata and controls
58 lines (47 loc) · 1.64 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
let randomNum;
let lives = 10;
const startBtn = document.getElementById("start-btn");
const restartBtn = document.getElementById("restart-btn");
const guessBtn = document.getElementById("submit-btn");
const gameDisplay = document.getElementById("game");
const hintText = document.getElementById("text");
let userGuess;
const userLife = document.getElementById("count");
function startGame() {
gameDisplay.style.display = "flex";
startBtn.style.display = "none";
if (randomNum === undefined) {
randomNum = Math.floor(Math.random() * 100) + 1;
}
}
function checkGuess() {
userGuess = parseInt(document.getElementById("guess-input").value);
if (userGuess === randomNum) {
hintText.innerText = "The Number is Correct!";
guessBtn.style.display = "none";
restartBtn.style.display = "inline-block";
} else if (userGuess < randomNum) {
hintText.innerText = "The Number is Higher";
loseLife();
} else if (userGuess > randomNum) {
hintText.innerText = "The Number is Lower";
loseLife();
}
}
function restartGame() {
lives = 10;
randomNum = Math.floor(Math.random() * 100) + 1;
userLife.innerText = lives;
guessBtn.style.display = "inline-block";
restartBtn.style.display = "none";
hintText.innerText = "Try to guess a number from 1-100!"
}
function loseLife() {
lives--;
userLife.innerText = lives;
if (lives === 0) {
hintText.innerText = "You ran out of lives. Game over!";
guessBtn.style.display = "none";
restartBtn.style.display = "inline-block";
}
}