var correctNumber = Math.floor(Math.random() * 15) + 1;
var turns = 5;
var userChoice = "Guess a number, 1-15!" + " ";
while (turns > 0) {
var guess = prompt(userChoice +
"You have" + " " + turns + " " + "guesses left.");
if (!guess) break;
guess = Number(guess);
if (isNaN(guess)) {
console.log("That's not a number! Try again but this time enter a number 1-15!");
turns = 0;
}
if (guess == correctNumber) {
console.log("You guessed the right number! You win!");
turns = 0;
}
else {
userChoice = "Try again!" + " ";
if (guess < correctNumber) userChoice += "That number is too small!" + " ";
if (guess > correctNumber) userChoice += "That number is too big!" + " ";
turns = turns - 1;
}
}
console.log ("Sorry, you have ran out of guesses. Hit refresh to try again.")
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.