Skip to content

Instantly share code, notes, and snippets.

@brookr
Created June 5, 2014 04:47
Show Gist options
  • Select an option

  • Save brookr/ffa5bacaa26b9f2ab071 to your computer and use it in GitHub Desktop.

Select an option

Save brookr/ffa5bacaa26b9f2ab071 to your computer and use it in GitHub Desktop.
<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