Skip to content

Instantly share code, notes, and snippets.

@rachelmyers
Created February 3, 2011 03:23
Show Gist options
  • Save rachelmyers/808991 to your computer and use it in GitHub Desktop.
Save rachelmyers/808991 to your computer and use it in GitHub Desktop.
Week 4 homework example for JS class
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Bubble Gum Game</TITLE>
<SCRIPT type="text/javascript">
//<![CDATA
function bubble()
{
var answer = Math.floor(Math.random()*21);
var guess = parseInt(prompt("How many blows before this bubble bursts?"));
var guesses = 0
var tries = 1
guesses += guess
// Guess was too small; let them guess again
while (guesses < answer) {
alert("You've blown " + guesses + " times, but the bubble still hasn't popped! Answer is " + answer );
guess = parseInt(prompt("How many more blows before this bubble bursts?"));
guesses += guess;
tries ++
}
if (tries > 1) {
var score = 21 - tries;
}
else {
var score = "'You didn't even make it out of the gate'"
}
// Guess was perfect.
if (guesses == answer) {
var replay = prompt("OMG! It was " + guesses + " blows before the bubble burst! Your score was " + score + " out of 20! Want to play again?");
}
// Guess was too big.
if (guesses > answer) {
var replay = prompt("Oh noes! It took just " + answer + " blows, not " + guesses + ". Your score was " + score + " out of 20! Want to play again?");
}
// Replay option.
if (replay == "yes"){
bubble();
}
else {
alert("Goodbye! It was fun playing with you!")
}
}
//]]>
</SCRIPT>
</HEAD>
<BODY onload="bubble();">
</BODY>
</HTML>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment