Skip to content

Instantly share code, notes, and snippets.

@macikokoro
Last active August 29, 2015 14:02
Show Gist options
  • Save macikokoro/9ef4443546fbbc9427c8 to your computer and use it in GitHub Desktop.
Save macikokoro/9ef4443546fbbc9427c8 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 number am I thinking about?");
difference = findDifference(answer, guess);
if (parseInt(guess) === answer) {
console.log("That was impressive!");
return "How did you do that?"
}
else {
return "Wrong again! You are off by :" + difference;
}
}
random = Math.floor(Math.random() * 20)
for (tries = 1; tries < 8; 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