Learning Javascript - Keep getting syntax errors in simple assignment!

Discussion in 'JavaScript' started by neha2011, Apr 26, 2011.

  1. #1
    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>
     
    neha2011, Apr 26, 2011 IP
  2. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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.
     
    Jan Novak, Apr 26, 2011 IP
  3. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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).
     
    AntelopeSalad, Apr 27, 2011 IP
  4. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Ahh, you are right, I have overlooked this.
     
    Jan Novak, Apr 27, 2011 IP
  5. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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):
     
    Cash Nebula, Apr 27, 2011 IP