var play, nam, secretNumber, counter, rannum, startMessage, newMessage;
while ((play = prompt('Want to play a guessing game?').toUpperCase()) == "YES") {
secretNumber = Math.ceil(Math.random() * 100);
alert('Great! The purpose of the game is to guess the number in as few guesses possible.');
counter = 0;
startMessage = 'Pick a number between 1 and 100';
newMessage = '';
nam = '';
while (nam != secretNumber) {
nam = prompt(nam + newMessage + startMessage + " (" + secretNumber + ")");
counter++;
if (nam < secretNumber) {
newMessage = ' is too LOW, guess again!\n\n';
} else if (nam > secretNumber) {
newMessage = ' is too HIGH, guess again!\n\n';
}
}
alert('YOU GOT IT! The secret number was ' + secretNumber + '!\n\n You got it in ' + counter + ' guesses');
}
//uncomment the counter check to have this only display if they say no the first time
if(play != "YES" && /*counter == undefined*/){
alert('Okay, maybe some other time then');
}
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.