Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Last active August 29, 2015 14:02
Show Gist options
  • Save jakehawken/f1601ae02e87a5390d01 to your computer and use it in GitHub Desktop.
Save jakehawken/f1601ae02e87a5390d01 to your computer and use it in GitHub Desktop.
<script>
var name, age, truth, playAgain, yesOrNo, randomNumber, guessingGame, game;
//takes yes/no input, sanitizes user input, returns yes or no
yesOrNo = function (input)
{
var output = input.toLowerCase();
while (output !== "yes" && output !== "no") //keeps re-asking you if you don't say Y or N
{
var output = (prompt("No, silly. Type 'yes' or 'no'")).toLowerCase();
}
return output;
}
//generates a random integer between a user-generated minimum and maximum
randomNumber = function (min, max)
{
return Math.floor(Math.random() * max) + min; //min is added because Math.floor rounds down and
}
//main game
guessingGame = function(minimum, maximum)
{
var actual = randomNumber(minimum, maximum);
var guess = prompt("I'm thinking of a number between " + minimum + " and " + maximum + ". Guess it! (You get 3 guesses!)");
for (i=1; i<4; i++)
{
if (guess != actual) //keeps re-asking you if you guess wrong
{
guess = prompt("Not quite. You were off by " + (Math.abs(actual-guess)) + ". Guess again.");
}
else if (guess == actual)
{
i=4;
}
}
if (guess == actual)
{
alert("Well done! You got it!");
}
else
{
alert("Sorry, you ran out of guesses!");
}
}
name = prompt("Well hello there! What's your name?");
age = prompt("Hi, " + name + "! Pleased to meet you. How old are you?");
truth = prompt("Really? " + age + "? Is that your real age?");
truth = yesOrNo(truth);
if (truth == "yes")
{
alert("Ok, I believe you. Click OK or hit Enter to get started.");
}
else
{
alert("Well, that was a weird thing to lie about... Alright, let's get started.");
}
game = guessingGame(1,10);
playAgain = yesOrNo(prompt("Would you like to play again?"));
while (playAgain=="yes")
{
game = guessingGame(1,10);
playAgain = prompt("Would you like to play again? (Yes or No)");
playAgain = (yesOrNo(playAgain));
}
alert("Thanks for playing! As a reward, here's a rad website!");
window.open("http://www.jakehawken.com");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment