-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (75 loc) · 2.42 KB
/
app.js
File metadata and controls
86 lines (75 loc) · 2.42 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
let start = document.querySelector(".start");
let input = document.querySelector(".numberInput");
let submit = document.querySelector(".submitBTN");
let b = document.querySelector("b");
let ol = document.querySelector("ol");
let msg = document.querySelector(".message");
let randomNumber = null;
let limit = 10;
// Sound files
let startSound = new Audio("sounds/start.mp3");
let winSound = new Audio("sounds/win.mp3");
let loseSound = new Audio("sounds/lose.mp3");
let wrongSound = new Audio("sounds/wrong.mp3");
let bgm = new Audio("sounds/bgm.mp3");
bgm.loop = true;
// Start / Restart Game
start.addEventListener("click", function () {
randomNumber = Math.floor(Math.random() * 100) + 1;
limit = 10; // ✅ reset limit
b.innerText = limit;
ol.innerHTML = "";
msg.textContent = "🎮 Game started! Make your guess.";
submit.disabled = false;
start.innerText = "Start Game";
start.style.display = "none";
bgm.currentTime = 0;
bgm.play();
startSound.play();
});
// Handle Guess Submission
submit.addEventListener("click", function () {
if (randomNumber == null) {
msg.textContent = "Please start the game first!";
input.value = "";
return;
}
let guess = Number(input.value.trim());
if (isNaN(guess) || guess <= 0 || guess > 100) {
msg.textContent = "⚠️ Please enter a valid number (1-100)!";
wrongSound.play();
input.value = "";
return;
}
// Add guess to list
let li = document.createElement("li");
li.innerText = guess;
ol.appendChild(li);
if (guess === randomNumber) {
msg.textContent = "🎉 Congratulations! You guessed it right!";
winSound.play();
bgm.pause();
submit.disabled = true;
start.innerText = "Restart";
start.style.display = "inline-block";
}
else if (guess < randomNumber) {
msg.textContent = "⬆️ Your guess is too low...";
wrongSound.play();
b.innerText = --limit;
}
else {
msg.textContent = "⬇️ Your guess is too high...";
wrongSound.play();
b.innerText = --limit;
}
if (limit <= 0 && guess !== randomNumber) {
msg.textContent = "❌ Game Over! Better luck next time!";
loseSound.play();
bgm.pause();
submit.disabled = true;
start.innerText = "Restart";
start.style.display = "inline-block";
}
input.value = "";
});