Skip to content

Instantly share code, notes, and snippets.

@petemmitchell
Created June 9, 2014 00:58
Show Gist options
  • Select an option

  • Save petemmitchell/c5fe64685fb7e0f3df46 to your computer and use it in GitHub Desktop.

Select an option

Save petemmitchell/c5fe64685fb7e0f3df46 to your computer and use it in GitHub Desktop.
CodeFellows F1: Project 2 (Guessing game w/ For loop)
<script>
var answer;
var guessingGame = function(answer) {
var guess, difference;
guess = prompt("I'm thinking of a number between 1 & 20. Can you guess what it is?")
difference = Math.abs(guess - answer)
if(guess == answer) {
alert("You win a new car!")
return;
}
else
if (difference >= 6) {
alert("You're getting cold.")
}
else
if (difference >= 3 && difference <= 5) {
alert("You're getting warm.")
}
else
if (difference >= 1 && difference <= 2) {
alert("You're getting hot.")
}
}
answer = Math.floor(Math.random() * 20)
for (tries = 1; tries < 4; tries++) {
result = guessingGame(answer)
alert(result);
}
</script>
@petemmitchell
Copy link
Author

Not sure why I'm getting the 2nd alert with "undefined". Been trying to work it out, but it's a head-scratcher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment