In: Computer Science
<!DOCTYPE html>
<html>
<head>
<title>Guess The Number</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js"></script>
</head>
<body class="text-center">
<nav class="navbar navbar-dark bg-dark">
<!-- Navbar content -->
<a class="navbar-brand" href="#">Guess The Number</a>
</nav>
<main>
<h1 class="banner p-3">Can u guess the number that computer has selected?</h1>
<div class="form-group ">
<input id="number-guess" class="form-control form-control-lg" type="number" placeholder="What's your guess?">
</div>
<div id="result">
<!-- This is where the result will be-->
</div>
<button type="button" id="number-submit" class="btn btn-lg btn-secondary">Check Me</button>
<button type="button" id="restart-game" class="btn btn-lg btn-light">Restart</button>
<div id="history">
<!-- This is where the guess history will be-->
</div>
</main>
</body>
</html>
style.css
#number-guess, #result, #history {
max-width: 400px;
margin: 0 auto;
}
#history {
margin-top: 16px;
}
.banner {
color:#070d4f;
font-size: 30px;
font-family: sans-serif;
}
script.js
/**
* Guess The Number Game
*/
// Variable to store the list of guesses
let guesses;
// Variable for store the correct random number
let correctNumber;
window.onload = function () {
initGame();
document.getElementById("number-submit").addEventListener("click", playGame);
document.getElementById("restart-game").addEventListener("click", initGame);
};
function playGame() {
let numberGuess = document.getElementById("number-guess").value;
saveGuessHistory(numberGuess);
displayHistory();
displayResult(numberGuess);
}
// Initialize a new game by resetting all values and content on the page
function initGame() {
correctNumber = getRandomNumber();
guesses = [];
displayHistory();
resetResultContent();
}
// Reset the results list display
function resetResultContent() {
document.getElementById("result").innerHTML = "";
}
// Return random number between 1 and 100
function getRandomNumber() {
/**
* Math.random returns a number between 0 and 1,
* and that's why we multiply it by 50
*/
return Math.floor(Math.random() * 50 + 1);
}
// Save the user guess entered from the input
function saveGuessHistory(guess) {
guesses.push(guess);
}
// Display history in HTML
function displayHistory() {
let index = guesses.length - 1;
let list = "<ul class='list-group'>";
while (index >= 0) {
list +=
"<li class='list-group-item'>" +
"You guessed " +
guesses[index] +
"</li>";
index -= 1;
}
list += "</ul>";
document.getElementById("history").innerHTML = list;
}
// Display the result in HTML
function displayResult(numberGuess) {
if (numberGuess > correctNumber) {
showNumberAbove();
} else if (numberGuess < correctNumber) {
showNumberBelow();
} else {
showYouWon();
}
}
// Retrieve the dialog based on if the guess is wrong or correct
function getDialog(dialogType, text) {
let dialog;
switch (dialogType) {
case "warning":
dialog = "<div class='alert alert-warning' role='alert'>";
break;
case "won":
dialog = "<div class='alert alert-success' role='alert'>";
break;
}
dialog += text;
dialog += "</div>";
return dialog;
}
function showYouWon() {
const text = "Awesome job, you got it!";
let dialog = getDialog("won", text);
document.getElementById("result").innerHTML = dialog;
}
function showNumberAbove() {
const text = "Your guess is too high!";
let dialog = getDialog("warning", text);
document.getElementById("result").innerHTML = dialog;
}
function showNumberBelow() {
const text = "Your guess is too low!";
let dialog = getDialog("warning", text);
document.getElementById("result").innerHTML = dialog;
}