SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    Santa Cruz
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    <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>
    "The absence of flaw in beauty is itself a flaw." Havelock Ellis

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    Santa Cruz
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    "The absence of flaw in beauty is itself a flaw." Havelock Ellis

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Marple View Post
    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?
    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 }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Marple View Post
    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?
    Let's simplify, so that we can see the structure more clearly.

    Code javascript:
    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:

    Code javascript:
    if (condition) {
     
    } else if (condition) {
     
    } else {
     
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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>

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    Santa Cruz
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!
    "The absence of flaw in beauty is itself a flaw." Havelock Ellis

  8. #8
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
     
    <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>

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    Santa Cruz
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all again, and thanks Kalon for fixing it- that was the last of thirteen programs due by Sunday, sweet relief!
    "The absence of flaw in beauty is itself a flaw." Havelock Ellis


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •