-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_program_02.html
More file actions
29 lines (29 loc) · 848 Bytes
/
js_program_02.html
File metadata and controls
29 lines (29 loc) · 848 Bytes
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
<!DOCTYPE html>
<html>
<head>
<title>Guess the Number Game</title>
</head>
<body>
<h2>Main Program: Guess the Number Game</h2>
<input type="number" id="mainInput" placeholder="Enter your guess">
<button onclick="playGame()">Guess</button>
<p id="feedback"></p>
<p id="attempts"></p>
<script>
let target = Math.floor(Math.random() * 100) + 1;
let count = 0;
function playGame() {
let guess = parseInt(document.getElementById("mainInput").value);
count++;
if (guess < target) {
document.getElementById("feedback").innerText = "Too Low";
} else if (guess > target) {
document.getElementById("feedback").innerText = "Too High";
} else {
document.getElementById("feedback").innerText = "Correct!";
document.getElementById("attempts").innerText = "You guessed it in " + count + "attempts.";
}
}
</script>
</body>
</html