Learning Javascript - Keep getting syntax errors in simple assignment!

I was wondering if anyone might take a quick look at this program I’m working on for a beginner class…it’s brief. I keep getting syntax errors and cannot figure out why. All Firebug tells me is that I have a ‘syntax error’ for one of the ‘else’ statements.

Would really appreciate it, pretty please :slight_smile:

<html>
<body>
<pre>
<script type=“text/javascript”>

// initalizing the values of three variables
var red = “X”;
var blue = “O”;
var green = “X”;

// Per the statements below, check first for three X’s
// If the criteria isn’t met, move onto three O’s
// If neither condition is met, display “Cat’s game”

if [SIZE=2]COLOR=#0000f0[/COLOR][/SIZE]; {
window.document.writeln('X wins!');

} else {

if [SIZE=2]COLOR=#0000f0[/COLOR][/SIZE];
window.document.writeln('O wins!');

} else {

window.document.writeln('Cat’s game!')
}

</script>
</pre>
</body>
</html>

An easy way to keep track of the brackets is to label them like this:
{ //1a


} //1b
You can then see when one or more are missing. I have labelled your script in this way to show you how it works. Once you have established your logic you can remove them.
Other things that you need to watch are: There was no <head></head> in your html doc. Also, you put a semi-colon after your if statements like this:
if ((red=“X”)&&(blue=“X”)&&(green=“X”)); {
This is probably the source of yourr syntax errors. See your script below for the details:

<html>

<head>

</head>

<body>

<script type=“text/javascript”>
<!–
// initalizing the values of three variables
var red = “X”, blue = “O”, green = “X”;
//
// Per the statements below, check first for three X’s
// If the criteria isn’t met, move onto three O’s
// If neither condition is met, display “Cat’s game”

if ((red=“X”)&&(blue=“X”)&&(green=“X”))
{//1a
window.document.writeln(‘X wins!’);
}//1b
else
{ //2a
if ((red=“O”)&&(blue=“O”)&&(green=“O”))
{ //3a
window.document.writeln(‘O wins!’);
} //3b
else
{ //4a
window.document.writeln(‘Cat’s game!’)
} //4b
} //2b

//
//–>
</script>

</body>

</html>

Thank you, that was so quick!

But I do count three sets for a total of 6, 3 opening and 3 closing… does anyone else see a rogue bracket?

Thank you all again, and thanks Kalon for fixing it- that was the last of thirteen programs due by Sunday, sweet relief!

This code now works.

You had a missing } at the end of your script and you need == instead of just = when you compare to variables and I took out the ; after your if conditions.

You should also include a <title> and <head> as othjers have stated

 
<html>
<body>
<pre>
<script type="text/javascript">
// initalizing the values of three variables
var red = "X";
var blue = "O";
var green = "X";
// Per the statements below, check first for three X's
// If the criteria isn't met, move onto three O's
// If neither condition is met, display "Cat's game"
if ((red[COLOR=red]==[/COLOR]"X")&&(blue="X")&&(green[COLOR=red]==[/COLOR]"X")) {
window.document.writeln('X wins!');
} else {
       if ((red[COLOR=red]==[/COLOR]"O")&&(blue[COLOR=red]==[/COLOR]"O")&&(green[COLOR=red]==[/COLOR]"O")) {
             window.document.writeln('O wins!');
       } else {
             window.document.writeln([COLOR=red]"[/COLOR]Cat's game![COLOR=red]"[/COLOR]);
       }
[COLOR=red]}[/COLOR]
</script>
</pre>
</body>
</html>

Thanks for all your help everyone- I really appreciate you taking the time to help out a noob. Think I’ll start over from scratch and see if I can make it work with your suggestions & corrections.

Have a great weekend!

Let’s simplify, so that we can see the structure more clearly.


if (condition); {

} else {

if (condition);

} else {

}

Those semicolons should not be there, as the if statement then ends at that point.

You may instead be wanting this structure instead:


if (condition) {

} else if (condition) {

} else {

}

Oops - the } on the front of the second else also doesn’t belong there since with that there the else has no if to match to (I didn’t look past the unnecessary { to see the unnecessary }). So the original code has a } misplaced rather than missing.

The simplest way to tell when this occurs is to always indent your code when you use a { so as to make it clear where the matching } is. The other alternative is to feed it through my formatter at http://javascript.about.com/library/blformat.htm which will indent for you and thus make it obvious which { are being matched to what }

With the first of the lines that says:

} else {

remove the { or add an extra } to the end of the script to match with it.

If you count the number of { and the number of } you’ll see that they don’t match.