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 <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="X")&&(blue="X")&&(green="X")); { window.document.writeln('X wins!'); } else { if ((red="O")&&(blue="O")&&(green="O")); window.document.writeln('O wins!'); } else { window.document.writeln('Cat's game!') } </script> </pre> </body> </html>
Do not use semicolon after you specify a condition: instead of if ((red="X")&&(blue="X")&&(green="X")); { Code (markup): write if ((red="X")&&(blue="X")&&(green="X")) { Code (markup): You can specify only one else statement. You have two statements in you if. You can use else if. There is also a problem with aposthrophe, you must escape it in string. Instead of window.document.writeln('Cat's game!') Code (markup): write window.document.writeln('Cat\'s game!') Code (markup): Also your second block not started with { So the whole correct code should be: if ((red="X")&&(blue="X")&&(green="X")) { window.document.writeln('X wins!'); } else if ((red="O")&&(blue="O")&&(green="O")){ window.document.writeln('O wins!'); } else { window.document.writeln('Cat\'s game!') } Code (markup): Please use editors Netbeans or Eclipse, which will tell you exactly where you have a syntax bugs in the code before you run the code in the browser.
Since it's an assignment I won't give you a full solution but when comparing values in the order you're trying to do it you need to be using the == or === operator in your if statements. = is used to assign a value. == is used to compare 2 values. === is the same as == except it uses more strict rules (ex. "1" == 1 will result in true but "1" === 1 will result in false).
Another useful tip is put the variable last so it doesn't get reassigned if you mix up the operators. It will cause an error instead of passing through as normal. if (x = 5) // Bad if (5 = x) // Not so bad <html> <head> <script type="text/javascript"> var x = 1; document.write('x = ' + x + '<br />'); if (x = 2) document.write('if statement returns true!<br />'); document.write('x = ' + x); </script> </head> <body> </body> </html> Code (markup):