Log in

View Full Version : guys, review my javascript 'cause it isn't working :(


bigtimecow
Feb 10th, 2006, 10:21 AM
<html>
<head>
<title>Adam Norton's Favorite Color Guessing Game</title>
</head>
<body>
<script language="Javascript">
<!--
var guessColor = prompt("What is Adam Norton's favorite color? (guess 1)","");
if (guessColor == "red")
{
alert("That is correctamundo! You are the master of guessing colors!");
document.write("<h1><font color="red">YOU GUESSED CORRECTLY!</font></h1>");
}
else
{
alert("That is incorrect! My favorite color is hotter than that...");
var guessColor = prompt("What is Adam Norton's favorite color? (guess 2)","");
if (guessColor == "red")
{
alert("That is correctamundo! You are pretty good at guessing colors!");
document.write("<h1><font color="red">YOU GUESSED CORRECTLY!</font></h1>");
}
else
{
alert("That is incorrect! My favorite color reminds people of anger...");
var guessColor = prompt("What is Adam Norton's favorite color? (guess 3 - the last!)","");
if (guessColor == "red")
{
alert("That is correctamundo! You aren't very good at guessing colors, but you got it!");
document.write("<h1><font color="red">YOU GUESSED CORRECTLY!</font></h1>");
}
else
{
alert("That is incorrect! Sorry, that was your final try. The correct answer is red.");
}
}
}

document.write("<h2>If you would like to play again, please press F5.</h2>");
//-->
</script>

</body>
</html>


:(

AChimp
Feb 10th, 2006, 10:26 AM
Good God that's the worst code I've ever seen. Look up for-loops. :x

AChimp
Feb 10th, 2006, 10:28 AM
And HTML won't show up inside an alert box.

bigtimecow
Feb 10th, 2006, 10:31 AM
jesus, i just started my class :(

ahh, but would that be the problem?

AChimp
Feb 10th, 2006, 11:03 AM
You're declaring guessColor multiple times, for starters.

bigtimecow
Feb 10th, 2006, 12:48 PM
ahh yes, you're right. what if i get rid of the "var" before each guessColor after the first declaration?

Esuohlim
Feb 10th, 2006, 01:17 PM
This reminds me of trying to learn MATLAB :(

AChimp
Feb 10th, 2006, 01:18 PM
ahh yes, you're right. what if i get rid of the "var" before each guessColor after the first declaration?

It might work after you do that.

The best way to debug Javascript is with Firefox and its javascript console. It'll tell you exactly what errors it encounters.

bigtimecow
Feb 10th, 2006, 03:44 PM
well, now that i'm home (i was at school) i'll test it in mozilla

bigtimecow
Feb 10th, 2006, 03:51 PM
<html>
<head>
<title>Adam Norton's Favorite Color Guessing Game</title>
</head>
<body>
<script language="Javascript">
<!--
var guessColor = prompt("What is Adam Norton's favorite color? (guess 1)","");
if (guessColor == "red")
{
alert("That is correctamundo! You are the master of guessing colors!");
document.write("<h1><font color=#FF0000>YOU GUESSED CORRECTLY!</font></h1>");
}
else
{
alert("That is incorrect! My favorite color is hotter than that...");
guessColor = prompt("What is Adam Norton's favorite color? (guess 2)","");
if (guessColor == "red")
{
alert("That is correctamundo! You are pretty good at guessing colors!");
document.write("<h1><font color=#FF0000>YOU GUESSED CORRECTLY!</font></h1>");
}
else
{
alert("That is incorrect! My favorite color reminds people of anger...");
guessColor = prompt("What is Adam Norton's favorite color? (guess 3 - the last!)","");
if (guessColor == "red")
{
alert("That is correctamundo! You aren't very good at guessing colors, but you got it!");
document.write("<h1><font color=#FF0000>YOU GUESSED CORRECTLY!</font></h1>");
}
else
{
alert("That is incorrect! Sorry, that was your final try. The correct answer is red.");
}
}
}

document.write("<h2>If you would like to play again, please press F5.</h2>");
//-->
</script>

</body>
</html>


yay it works (it looks really messy because there ain't no damn indentation here :( )

thank you achimp