Leap Year dertermination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brigitte Behrmann
    New Member
    • Jun 2007
    • 25

    Leap Year dertermination

    Can anyone assist with my code. I just cannot get this to work:
    I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a form with a single text box and using an alert dialog box stating if the entered year is a standard or leap year.

    Code so far:
    [HTML]<HTML>
    <HEAD>
    <TITLE>Is it a leap year?</TITLE>
    </HEAD>

    <SCRIPT LANGUAGE="JAVAS CRIPT>
    <!-- Hide from old browsers

    function leapYear(){
    Last2Digits = year % 100
    if (Last2Digits == 0):
    flag = year % 400
    else:
    flag = year % 4

    if (flag == 0) :
    window.alert("T he year you have entered is a leap year.")
    else:
    window.alert("T he year you have entered is a standard year.")
    }

    //-->
    </script>
    </head>
    <body>
    <form><p>Year : <input type="text" name="Year"></p>
    <input type="button" value="Check for Leap Year" name="check for leap year" onclick="leapYe ar()">
    </form>
    </body>[/HTML]
    Last edited by gits; Jul 25 '07, 10:54 AM. Reason: added CODE tags
  • jsakalos
    New Member
    • Jun 2007
    • 12

    #2
    There is one trick you could use with Date object. First set date to "Feb 29, YYYY" and then get month from the same Date object. If you get Feb it's leap year, if you get March it's not.

    Beware, months are numbered from 0 - 11.

    Comment

    • Brigitte Behrmann
      New Member
      • Jun 2007
      • 25

      #3
      Nope, you've lost me there... anyone else got some ideas for a simple fix. The question is only worth 15 marks, but with the amount of time I have spent on this it shoud have been 150!!!

      Code:
      <HTML>
      <HEAD>
      <TITLE>Is it a leap year?</TITLE>
      </HEAD>
      
      <SCRIPT LANGUAGE="JAVASCRIPT>
      <!-- Hide from old browsers
      
      function leapYear(){
         Last2Digits = year % 100
         if (Last2Digits == 0):
             flag = year % 400
         else:
          flag = year % 4
      
         if (flag == 0) :
             window.alert("The year you have entered is a leap year.")
         else:
             window.alert("The year you have entered is a standard year.")
      }
      
      //-->
      </script>
      </head>
      <body>
      <form><p>Year: <input type="text" name="Year"></p>
      <input type="button" value="Check for Leap Year" name="check for leap year" onclick="leapYear()">
      </form>
      </body>

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        the following works your way:

        [HTML]<title>Is it a leap year?</title>
        <head>
        <script>
        function leapYear(year) {
        var Last2Digits = year % 100;
        if (Last2Digits == 0) {
        flag = year % 400;
        } else {
        flag = year % 4;
        }

        if (flag == 0) {
        window.alert("T he year you have entered is a leap year.");
        } else {
        window.alert("T he year you have entered is a standard year.");
        }
        }
        </script>
        </head>
        <body>
        <form>
        <p>Year:
        <input type="text" id="year" name="Year">
        </p>
        <input type="button" value="Check for Leap Year" name="check_for _leap_year" onclick="leapYe ar(document.get ElementById('ye ar').value);">
        </form>
        </body>
        [/HTML]

        please compare it to your not-working code ... have a look at your head-tags, correct your if-statements, pass the year to your function until it expects year to work with ...

        the errors you make are really simple ... and i would recommend that you double-check the syntax and the used values and variables ... try!!! to use firefox and the firebug-extension that shows you instantly on testing, whats wrong with your code ...

        kind regards ...

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          in case you wanted to use the conditional operator use it the following way:

          [CODE=javascript]
          var flag = Last2Digits == 0 ? year % 400 : year % 4;
          [/CODE]

          this usually is used for assigning a value to a variable based on a condition.

          kind regards ...

          Comment

          • jsakalos
            New Member
            • Jun 2007
            • 12

            #6
            Can anything be simpler than this?

            [php]
            var isLeap = function(year) {
            return new Date('Feb 29, ' + year).getMonth( ) === 1;
            };
            // usage
            alert(isLeap(20 00));
            alert(isLeap(20 01));
            [/php]

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              Originally posted by jsakalos
              Can anything be simpler than this?

              [php]
              var isLeap = function(year) {
              return new Date('Feb 29, ' + year).getMonth( ) === 1;
              };
              // usage
              alert(isLeap(20 00));
              alert(isLeap(20 01));
              [/php]
              you're right ... that's a slick way to achieve the goal. but let us try to help her with the code she has already written ... until she starts with javascript it is an good idea to let her code and correct the errors ... so that she gets the feeling and the ideas of what to do ... but i agree ... your code is real cool ;)

              kind regards ...

              Comment

              • jsakalos
                New Member
                • Jun 2007
                • 12

                #8
                OK,

                let's let her decide. You see, I'm a kind of guy that looks for solutions. So I wouldn't insist on my 100 lines of code if I had a 3 lines replacement.

                Nevertheless, It's good to learn to make those 100 lines bugfree. :)

                Comment

                • Brigitte Behrmann
                  New Member
                  • Jun 2007
                  • 25

                  #9
                  Originally posted by jsakalos
                  OK,

                  let's let her decide. You see, I'm a kind of guy that looks for solutions. So I wouldn't insist on my 100 lines of code if I had a 3 lines replacement.

                  Nevertheless, It's good to learn to make those 100 lines bugfree. :)
                  Hi Guys
                  I am in no position to argue. At the end of the day I have an assignment that I have to submit using JavaScript and it has to be posted off tomorrow, so whatever you can help me with will be great. I have printed all your responses and filed to keep as reference with my studies, but if I do not submit the assignments I will not get sufficient credits to even qualify to write the exam at the end of the year... so PLEASE HELP... whatever you got as long as long as it is JS code I'm cool!

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    of course and i fully agree with that ... but one thing too: there are no 100 lines ;) ... and of course it is her decision ... and nobody insists ;) but: she used the if-statement incorrect ... and with showing her an entire new solution ... we wouldn't help her to learn the correct usage ... do you know what i mean? ...

                    @brigitte: the solution that jsakalos showed is for some reasons better then ours ;) ... the main reason is: it is shorter ... and typically in real-world projects we try to write as little code as we can do ... this is for maintainance reasons in case of adaptions as well as for errors ... more lines of code may produce more errors ... ok? ...

                    kind regards ...

                    Comment

                    • Brigitte Behrmann
                      New Member
                      • Jun 2007
                      • 25

                      #11
                      Originally posted by gits
                      of course and i fully agree with that ... but one thing too: there are no 100 lines ;) ... and of course it is her decision ... and nobody insists ;) but: she used the if-statement incorrect ... and with showing her an entire new solution ... we wouldn't help her to learn the correct usage ... do you know what i mean? ...

                      @brigitte: the solution that jsakalos showed is for some reasons better then ours ;) ... the main reason is: it is shorter ... and typically in real-world projects we try to write as little code as we can do ... this is for maintainance reasons in case of adaptions as well as for errors ... more lines of code may produce more errors ... ok? ...

                      kind regards ...
                      Agreed, I will give it a try after I have finished making dinner.... which is probably burning in the oven as we speak.... watch this space in case I get stuck!

                      Thanks - BB

                      Comment

                      • jsakalos
                        New Member
                        • Jun 2007
                        • 12

                        #12
                        Originally posted by gits
                        of course and i fully agree with that ... but one thing too: there are no 100 lines ;) ... and of course it is her decision ... and nobody insists ;) but: she used the if-statement incorrect ... and with showing her an entire new solution ... we wouldn't help her to learn the correct usage ... do you know what i mean? ...
                        kind regards ...

                        Come on gits,

                        the last thing I want is to argue... And first thing I want is to help (and get helped when I need it).

                        PS: 100 was an example; just because it's nice number. I'm too lazy to count Brigitte's lines. ;)

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5388

                          #13
                          ok ;) ... if you insist ... there are 13 of her lines against 3 of yours ... and as i said ... yours are better ... but there is nothing to argue that your solution doesn't show her the correct usage of an if-statement ... that is a basic construct in every programming language and should be corrected ...

                          you helped a lot with your solution ... it shows that often there is a much better solution to do things ... you are right with what you said ... and i think she could solve her problem now ... good job!

                          kind regards ...

                          Comment

                          Working...