JavaScript syntax problems with simple program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naomi81
    New Member
    • Jan 2008
    • 5

    JavaScript syntax problems with simple program

    Hello All!

    I'm brand new to JavaScript, and i've been set the challenge of writing my first program. It's to classify eggs by their size....I've been sat here for about three hours now and cannot see why it won't run. I'm sure i've made an obvious mistake but i just can't see it.....Can someone kindly help?

    [HTML]<HTML>
    <HEAD>
    <TITLE>
    Egg sizes
    </TITLE>
    <SCRIPT LANGUAGE = "JavaScript ">
    //A Program for classifying eggs by size

    var weight;

    weight = window.prompt(' Please enter the weight of the egg in grams', '');
    weight = parseFloat(weig ht);
    if (weight <53)
    {
    document.write( 'This egg is small.')
    }
    else
    {
    if (weight >= 53) && (weight <63)
    {
    document.write( 'This egg is medium.')
    }
    ...[snipped]...
    document.write( That was Eggscellent, Thank you for using this program.'

    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>[/HTML]
    Last edited by acoder; Jan 18 '08, 10:16 AM. Reason: Added code tags + removed some code
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What defines the weight of the Egg, eggzactly?

    I'm sorry! It had to be done!

    Forget about the question! I just read a little more..

    Try changing:
    Code:
    script language="javascript"
    to
    Code:
    script type="text/javascript"
    The 'language' attribute is now deprecated.

    Comment

    • Naomi81
      New Member
      • Jan 2008
      • 5

      #3
      Thanks so much for replying...I've just tried that but still no joy?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        No problem!

        After having a glance over your, not so pretty, code, i found some problems!

        When you, in an if(else) statement, have something like || / && you need an extra set of parenthesis' for each || // && you use - you'll see what i mean in the revised code.

        I also changed it to if.. else if... else statements. I also made the code look nicer.. easier to debug, exactly like in these situations!
        [code=javascript]
        <script type="text/javascript">
        //A Program for classifying eggs by size

        var weight;

        weight = window.prompt(' Please enter the weight of the egg in grams', '');
        weight = parseFloat(weig ht);
        if (weight <53)
        {
        document.write( 'This egg is small.')
        }
        else if((weight >= 53) && (weight <63))
        {
        document.write( 'This egg is medium.')
        }
        [snipped]
        else{
        document.write( "Reload.")
        }
        document.write( "That was Eggscellent, Thank you for using this program.")

        </script>
        [/code]
        Anything you want answering just ask :)
        Last edited by acoder; Jan 18 '08, 10:14 AM. Reason: removed some code

        Comment

        • Naomi81
          New Member
          • Jan 2008
          • 5

          #5
          Oh my god..Thank you so much it's amazing!!

          One thing though, we have not yet learnt the reload code. What does this do and how can I express this another way..

          Thanks again. Was it that really that awful for a first try?!!!

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            The document.write( "Reload.")?

            That's really of no relevance, it's not even a javascript function; it's just good practice to end an IF statement with an ELSE statement.

            Don't worry about it.

            :)

            Post back whenever you need some help!

            Please also remember to use the relevant CODE tags. ie. [ CODE=javascript]javascript code goes here...[/CODE]

            Comment

            • Naomi81
              New Member
              • Jan 2008
              • 5

              #7
              Thanks so much for helping me!!

              Just one final thing (sorry i'm a pain)

              Is it possible to end the code without the reload expression. Purely because i need to show this to my tutor, and he knows we haven't learnt it yet!!!

              I really appreciate your kindness, I've learnt more in the last hour than I have in the last week!!!!

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Naomi81
                Thanks so much for helping me!!

                Just one final thing (sorry i'm a pain)

                Is it possible to end the code without the reload expression. Purely because i need to show this to my tutor, and he knows we haven't learnt it yet!!!

                I really appreciate your kindness, I've learnt more in the last hour than I have in the last week!!!!
                There is no 'reload' expression!

                It's just a string of text.. it's meaningless; it has no meaning; no javascript-function; just random text!

                Haha.

                And oh dear... i've just written your homework!
                Aslong as you've understood and improved your javascript with this, i can sleep tonight?

                Comment

                • Naomi81
                  New Member
                  • Jan 2008
                  • 5

                  #9
                  It's ok you can sleep tonight!!
                  I'm a mature (well 26 yr old) student taking an evening class....!
                  Thanks so much for your help , it's definitely making sense now!

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    We have quite a strict policy on homework/class assignments - please read that section of the guidelines. I'm removing parts of the code as per the guidelines.

                    MODERATOR

                    Comment

                    Working...