fceux/new/Javascript/youGuessed.html

81 lines
2.1 KiB
HTML

<html>
<head>
<title>Guess</title>
<style type="text/css">
.style1 {
text-align: center;
}
</style>
</head>
<body>
<h2 class="style1">Guessing Game</h2>
<h3 class="style1">Can you guess the number?</h3>
<h4 class="style1">How many tries will it take!</h4>
<script language= "javascript" type="text/javascript">
var range = 100 //The range of possible numbers the guessing game will use
var target = Math.floor(Math.random() * range + 1);
var count = 1 //Number of tries the user has taken
var guess = 0 //The users current guess
var win = false //Bool that will be true when the user makes the correct guess
var test = 0 //temp variable for determining how close the user got
var resultStr = "" //Use to create a message to the user when they guess correctly
var guesses = new Array() //Array used to store the user's guesses
//document.write("Debug: target = ", target, "<br>")
document.write("Number range: 0-", range, "<br><br>")
do
{
guess = prompt("Enter a guess between 1 and " + range)
guesses[count] = guess //Store the user's guess
if (guess == target)
{
//You win
win = true
if (count < 5)
{
resultStr = "You did an awesome job, it only took you " + count + " tries to guess the target number."
}
else if (count <= 15)
{
resultStr = "Goodness. It took you " + count + " tries to guess the target number."
}
else
{
resultStr = "Yikes! It took you " + count + " tries to guess the target number."
}
}
else
{
count++
test = guess - target
if (test < 3 && test > -3)
{
document.write(guess, ": You're Red Hot!<br>")
}
else if (test < 10 && test > -10)
{
document.write(guess, ": You're getting warm.<br>")
}
else if (test < 20 && test > -20)
{
document.write(guess, ": You're getting cold!<br>")
}
else
{
document.write(guess, ": You're Ice Cold!<br>")
}
}
} while (!win)
alert("Success!\n" + resultStr)
document.write("Your ", guesses.length, " guesses: ", guesses, "<br>")
</script>
</body>
</html>