-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNUMBER GUESSING GAME JAVASCRIPT.txt
More file actions
193 lines (165 loc) · 5.28 KB
/
NUMBER GUESSING GAME JAVASCRIPT.txt
File metadata and controls
193 lines (165 loc) · 5.28 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
let secretNumber, min, max, attempts, maxAttempts, hintUsed;
let bestScores = { easy: null, medium: null, hard: null };
const message = document.getElementById('message');
const hint = document.getElementById('hint');
const guessInput = document.getElementById('guess');
const submitBtn = document.getElementById('submit');
const restartBtn = document.getElementById('restart');
const attemptsDisplay = document.getElementById('attempts');
const bestScoreDisplay = document.getElementById('bestScore');
const difficultySelect = document.getElementById('difficulty');
const hintBtn = document.getElementById('hintBtn');
function initGame() {
const difficulty = difficultySelect.value;
if (difficulty === 'easy') {
min = 1; max = 50; maxAttempts = 10;
} else if (difficulty === 'medium') {
min = 1; max = 100; maxAttempts = 7;
} else {
min = 1; max = 200; maxAttempts = 5;
}
secretNumber = Math.floor(Math.random() * (max - min + 1)) + min;
attempts = 0;
hintUsed = false;
message.textContent = "";
hint.textContent = "";
attemptsDisplay.textContent = "";
bestScoreDisplay.textContent = bestScores[difficulty] ? `Best Score: ${bestScores[difficulty]} attempts` : "";
submitBtn.disabled = false;
guessInput.disabled = false;
guessInput.value = "";
guessInput.focus();
}
submitBtn.onclick = function() {
let guess = Number(guessInput.value);
if (!guess || guess < min || guess > max) {
message.textContent = `Enter a number between ${min} and ${max}.`;
message.style.color = "red";
return;
}
attempts++;
if (guess === secretNumber) {
message.textContent = ` You Win! You guessed it in ${attempts} attempts.`;
message.style.color = "green";
submitBtn.disabled = true;
guessInput.disabled = true;
const difficulty = difficultySelect.value;
if (!bestScores[difficulty] || attempts < bestScores[difficulty]) {
bestScores[difficulty] = attempts;
}
bestScoreDisplay.textContent = `Best Score: ${bestScores[difficulty]} attempts`;
hint.textContent = "";
} else if (attempts >= maxAttempts) {
message.textContent = ` You Lose! The number was ${secretNumber}.`;
message.style.color = "red";
submitBtn.disabled = true;
guessInput.disabled = true;
hint.textContent = "";
} else {
if (guess < secretNumber) {
min = guess + 1;
message.textContent = "Too low! Try again.";
} else {
max = guess - 1;
message.textContent = "Too high! Try again.";
}
message.style.color = "orange";
hint.textContent = `Hint: Try between ${min}–${max}`;
}
attemptsDisplay.textContent = `Attempts: ${attempts} / ${maxAttempts}`;
guessInput.value = "";
guessInput.focus();
};
hintBtn.onclick = function() {
if (hintUsed) {
alert("Hint already used!");
return;
}
const guess = Number(guessInput.value);
if (!guess) {
alert("Enter a number first to use hint!");
return;
}
const diff = Math.abs(secretNumber - guess);
if (diff === 0) {
alert("You already guessed it!");
return;
}
hint.textContent = diff <= 5 ? " Very close!" : "Not very close!";
hintUsed = true;
};
restartBtn.onclick = initGame;
difficultySelect.onchange = initGame;
initGame();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number Guessing Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="game-container">
<h1>Guess the Number!</h1>
<label for="difficulty">Select Difficulty:</label>
<select id="difficulty">
<option value="easy">Easy (1–50)</option>
<option value="medium" selected>Medium (1–100)</option>
<option value="hard">Hard (1–200)</option>
</select>
<p>Enter your guess:</p>
<input type="number" id="guess" placeholder="Your guess">
<button id="submit">Guess</button>
<button id="hintBtn">Get Hint</button>
<p id="message"></p>
<p id="hint"></p>
<p id="attempts"></p>
<p id="bestScore"></p>
<button id="restart">Restart</button>
</div>
<script src="script.js"></script>
</body>
</html>
body {
background: #f0f0f0;
font-family: Arial, sans-serif;
}
.game-container {
background: #fff;
padding: 30px;
border-radius: 12px;
max-width: 400px;
margin: 50px auto;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
text-align: center;
}
input[type="number"], select {
padding: 10px;
font-size: 16px;
width: 180px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 6px;
}
button {
padding: 10px 18px;
font-size: 16px;
margin: 5px;
cursor: pointer;
border: none;
border-radius: 6px;
background-color: #4cafef;
color: white;
}
button:hover {
background-color: #3a8cd6;
}
#message {
margin: 15px 0;
font-weight: bold;
}
#hint, #attempts, #bestScore {
font-style: italic;
color: #555;
}