Created
June 9, 2015 13:44
-
-
Save bronzehedwick/cd09336ecd105e2472bc to your computer and use it in GitHub Desktop.
Number guessing programming exorcise in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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