Skip to content

Instantly share code, notes, and snippets.

@makyo
Last active August 29, 2015 13:57
Show Gist options
  • Save makyo/9899912 to your computer and use it in GitHub Desktop.
Save makyo/9899912 to your computer and use it in GitHub Desktop.
alert('Think of a number from 1 to 100...');
var currGuess = 50;
do {
var response = prompt('I guess ' + currGuess + ' - am I too [h]igh, too [l]ow, or [r]ight on?');
if (response[0].toLowerCase() === 'h') {
currGuess--;
} else if (response[0].toLowerCase() === 'l') {
currGuess++;
} else {
break;
}
} while (true);
alert('Got it! Your number is ' + currGuess);
alert('Think of a number from 1 to 100...');
var currGuess = 50, max = 100, min = 0;
do {
var response = prompt('I guess ' + currGuess + ' - am I too [h]igh, too [l]ow, or [r]ight on?');
if (response[0].toLowerCase() === 'h') {
max = currGuess;
} else if (response[0].toLowerCase() === 'l') {
min = currGuess;
} else {
break;
}
currGuess = Math.round(min + (max - min) / 2);
} while (true);
alert('Got it! Your number is ' + currGuess);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment