Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created August 6, 2021 03:17
Show Gist options
  • Save natafaye/06da405755ce04e83e5fe13285b68373 to your computer and use it in GitHub Desktop.
Save natafaye/06da405755ce04e83e5fe13285b68373 to your computer and use it in GitHub Desktop.
let number = Math.ceil(Math.random() * 10)
alert("The secret number is " + number) // just for testing
let gameOn = true
while (gameOn) {
let guess = prompt("Guess a number between 0 and 10")
if (guess === null || guess === "null" || guess === ""){
alert("Game cancelled. Refresh to start again.")
gameOn = false
} else if (parseInt(guess) === number) {
alert("Congratulations, you guessed right.")
gameOn = false
} else if (parseInt(guess) != number) {
alert("Not quite, try again.")
}
}
console.log("Thanks for playing!")
const promptMessage = "What number am I thinking of?"
const answer = Math.floor(Math.random() * 10) + 1; // generate a random number from 1 to 10
// You could also use a do-while here and then you wouldn't need line 5
let guess = prompt(promptMessage);
while(parseInt(guess) !== answer) {
// Stop the loop if they cancel out
if(guess === null) {
alert("Goodbye!")
break;
}
guess = prompt(promptMessage);
}
// Only show the win message if they did actually win (meaning they didn't cancel out)
if(parseInt(guess) === answer) {
alert("You guessed it!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment