Created
June 5, 2014 04:47
-
-
Save brookr/ffa5bacaa26b9f2ab071 to your computer and use it in GitHub Desktop.
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
| <script> | |
| var random, result; | |
| var findDifference = function(answer, guess) { | |
| if (answer > guess) { | |
| return answer - guess; | |
| } | |
| else { | |
| return guess - answer; | |
| } | |
| } | |
| var guessingGame = function(answer) { | |
| var guess, difference; | |
| guess = prompt("What's my number?"); | |
| difference = findDifference(answer, guess); | |
| if (parseInt(guess) === answer) { | |
| console.log("Dagnabit, they got me!"); | |
| return "You win!"; | |
| } | |
| else { | |
| return "Off by: " + difference; | |
| } | |
| } | |
| random = Math.floor(Math.random() * 10) | |
| for (tries = 1; tries < 4; tries++) { | |
| result = guessingGame(random) | |
| alert(result); | |
| } | |
| </script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment