Skip to content

Instantly share code, notes, and snippets.

@bronzehedwick
Created June 9, 2015 13:44
Show Gist options
  • Save bronzehedwick/cd09336ecd105e2472bc to your computer and use it in GitHub Desktop.
Save bronzehedwick/cd09336ecd105e2472bc to your computer and use it in GitHub Desktop.
Number guessing programming exorcise in Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Guess My Number</title>
</head>
<body>
<script>
(function guessNumber() {
'use strict';
var number = Math.floor(Math.random() * (100 - 1) + 1),
answer = '',
moves = 0;
function ask(answer) {
if (answer === '') {
answer = prompt('Guess the number I\'m thinking of. It\'s between 1 and 100');
}
else if (answer === null) {
return;
}
else if (answer == number) {
alert('Yeah, that\'s it. You won. It only took you ' + moves + ' moves. I hope you\'re happy.');
return;
}
else if (answer < number) {
answer = prompt('Too low');
}
else if (answer > number) {
answer = prompt('Too high');
}
else {
answer = prompt('Not a number');
}
moves++;
ask(answer);
}
ask(answer);
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment