Skip to content

Instantly share code, notes, and snippets.

@sburbidg
Created June 11, 2014 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sburbidg/3b6ad650220659767a5b to your computer and use it in GitHub Desktop.
Save sburbidg/3b6ad650220659767a5b to your computer and use it in GitHub Desktop.
Guessing game after first objects lesson.
<script>
var collected = Math.floor(Math.random() * 1000)
function Range(low, high)
{
this.low = low
this.high = high
this.messageOutput = function(myGuess){return myGuess >= (collected + this.low) && myGuess <= (collected + this.high)
}
}
one = new Range( - 1000, - 800)
two = new Range( - 799, - 400)
three = new Range( - 399, - 50)
four = new Range( - 49, - 10)
five = new Range( - 9, - 1)
six = new Range( 1, 9)
seven = new Range( 10, 49)
eight = new Range( 50, 399)
nine = new Range( 400, 799)
ten = new Range( 800, 1000)
var butterfly = function() {
alert('Oh no, a bunch of butterflies escaped!')
}
var answer = confirm('Would you like to open the box?')
var done = false
while (!done)
{
if(answer)
{
butterfly()
done = true
}
else
{
var sure = confirm('Are you sure')
if(sure)
{
alert('Your loss, but really...')
}
else
{
butterfly()
done = true
}
}
}
alert('The butterflies need to be put back in the box so they don\'t die')
alert('There were 1000 in the box, collect as many as you can')
var guess = prompt('How many do you think you got back in the box?')
var correct = (guess == collected)
while(!correct)
{
if(one.messageOutput(guess))
{
guess = prompt('Wow, so far off, it looks like you caught way more than that.')
}
if(ten.messageOutput(guess))
{
guess = prompt('Wow, that\'s a little cocky, you didn\'t catch nearly that many.')
}
if(two.messageOutput(guess))
{
guess = prompt('Give yourself more credit than that.')
}
if(three.messageOutput(guess))
{
guess = prompt('Kinda close, try a little higher.')
}
if(four.messageOutput(guess))
{
guess = prompt('You are pretty close, you caught just a bit more than that.')
}
if(five.messageOutput(guess))
{
guess = prompt('I think it\'s just a few more.')
}
if(nine.messageOutput(guess))
{
guess = prompt('Haha, not nearly. Why don\'t you try a little lower.')
}
if(eight.messageOutput(guess))
{
guess = prompt('You didn\'t catch quite that many.')
}
if(seven.messageOutput(guess))
{
guess = prompt('You are pretty close, you caught just a bit less than that.')
}
if(six.messageOutput(guess))
{
guess = prompt('I think it\'s just a few less.')
}
if(guess == collected)
{
correct = true
}
}
alert('Awesome, all the butterflies you caught will be turned into makeup for human babies. When the United Butterflies finds out you will probably be tried for war crimes. Thanks for your allegiance to the Corporation.')
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment