0% found this document useful (0 votes)
115 views1 page

Guess Number Game in Javascript

This document contains code for a number guessing game that randomly generates a secret number between 1 and 100 and prompts the user to guess it. The code tracks the number of guesses and provides feedback if the guess is too low or high, declaring the user the winner if they correctly guess the secret number. It then prompts the user to play again, reloading the page if they choose yes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views1 page

Guess Number Game in Javascript

This document contains code for a number guessing game that randomly generates a secret number between 1 and 100 and prompts the user to guess it. The code tracks the number of guesses and provides feedback if the guess is too low or high, declaring the user the winner if they correctly guess the secret number. It then prompts the user to play again, reloading the page if they choose yes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

console.log("Welcome to the Number Guessing Game!

");

console.log("I'm thinking of a number between 1 and 100.");

console.log("Try to guess the number.");

var secretNumber = Math.floor(Math.random() * 100) + 1;

var attempts = 0;

var guessed = false;

while (!guessed) {

var guess = parseInt(prompt("Enter your guess:"));

attempts++;

if (guess < secretNumber) {

console.log("Too low! Try again.");

} else if (guess > secretNumber) {

console.log("Too high! Try again.");

} else {

console.log("Congratulations! You guessed the number.");

console.log("Number of attempts: " + attempts);

guessed = true;

var playAgain = prompt("Do you want to play again? (y/n):").toLowerCase();

if (playAgain === 'y') {

location.reload();

} else {

console.log("Thank you for playing!");

You might also like