determine leap year, javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ballygowanboy
    New Member
    • Jul 2007
    • 47

    determine leap year, javascript

    hi, i need a javascript that determines if a year is a leap year or not, by putting the year in a text field and hitting a submit button.

    i'm fairly new to all this, so would appriciate the help, ta.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    have a look at the following thread:



    kind regards

    Comment

    • ballygowanboy
      New Member
      • Jul 2007
      • 47

      #3
      Originally posted by gits
      hi ...

      have a look at the following thread:



      kind regards
      thanks gits, i'm using your code in that example, which works grand.

      can you tell me what the % sign does? year % 100

      also, what does flag mean?

      also, in the form submit button, can you explain this code,

      onclick="leapYe ar(document.get ElementById('ye ar').value);"

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        it's not my code ;) ... only helped to get it to work ... 3 things to mention:

        1. % is the modulus-operator - have a look here

        2. flag is a simple variable in that script ;) doesn't mean anything ... the poster there called it that way ...

        3. consider to use the solution that jsakalos provided there ... it's shorter probably better then the other solution

        kind regards

        Comment

        • ballygowanboy
          New Member
          • Jul 2007
          • 47

          #5
          Originally posted by gits
          hi ...

          it's not my code ;) ... only helped to get it to work ... 3 things to mention:

          1. % is the modulus-operator - have a look here

          2. flag is a simple variable in that script ;) doesn't mean anything ... the poster there called it that way ...

          3. consider to use the solution that jsakalos provided there ... it's shorter probably better then the other solution

          kind regards
          i think i'd better get my head around a basic way, before i use jsakalos's version.

          i have to do this in php.

          here's what i understand, the user has to enter a year, this number has to be disivable by 4 with no remainders, if the year ends in a 00 it's not a leap year, unless it's every 400 years, then it is.

          here's my own code so far,

          i'm parsing this through a form.....

          [CODE=php]<?php


          $leap = x % 100;



          $x = $_POST["txtRequest "];

          if ($leap == 0)

          {
          echo("this is a leap year");
          }


          else
          {

          echo("this is not a leap year");
          }

          ?>

          [/CODE]


          at the moment, i'm only dividing the number by 4, and seeing if the remainder is 0, this should work right? but it doesn't.

          i need to get that working, and then add 2 other variables?

          also, why doesn't flay have a var before it, if it is a variable?
          Last edited by gits; Jul 25 '07, 03:52 PM. Reason: added CODE tags

          Comment

          • ballygowanboy
            New Member
            • Jul 2007
            • 47

            #6
            ok, i was sure this would work........ but i'm just getting an error, am i close with this one, or am i way off?



            [CODE=php]<?php


            $leap = $x;



            $x = $_POST["year"];

            if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)

            {
            echo("this is a leap year");
            }


            else
            {

            echo("this is not a leap year");
            }

            ?>[/CODE]

            Comment

            • epots9
              Recognized Expert Top Contributor
              • May 2007
              • 1352

              #7
              Originally posted by ballygowanboy
              ok, i was sure this would work........ but i'm just getting an error, am i close with this one, or am i way off?



              [CODE=php]<?php


              $leap = $x;



              $x = $_POST["year"];

              if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)

              {
              echo("this is a leap year");
              }


              else
              {

              echo("this is not a leap year");
              }

              ?>[/CODE]
              what does the error say?

              and i thought u wanted to do this with javascript...th ats php?
              Last edited by gits; Jul 25 '07, 03:54 PM. Reason: fix CODE tags

              Comment

              • ballygowanboy
                New Member
                • Jul 2007
                • 47

                #8
                Originally posted by epots9
                what does the error say?

                and i thought u wanted to do this with javascript...th ats php?
                i have to do it in javascript and php.......... ok here's my latest attempt, in php........


                [CODE=php]<?php


                $leap = $x;



                $x = $_POST["year"];

                if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)

                {
                echo("this is a leap year");
                }


                else
                {

                echo("this is not a leap year");
                }

                ?>[/CODE]


                i'm not getting any errors, but basicly every year is a leap year :/

                can you comment on this code, tell me if i'm anywhere close, if i'm on the right track....... i know there's probably a cleaner faster way to do this, but i'm just trying to understand conditionals.

                basicly, i'm trying to say in the code, if x is divided by 4, 100 or 400, and you get a module with nothing left over, it's a leap year.

                am i making any sence?

                Comment

                • epots9
                  Recognized Expert Top Contributor
                  • May 2007
                  • 1352

                  #9
                  Originally posted by ballygowanboy
                  i have to do it in javascript and php.......... ok here's my latest attempt, in php........


                  [CODE=php]<?php


                  $leap = $x;



                  $x = $_POST["year"];

                  if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)

                  {
                  echo("this is a leap year");
                  }


                  else
                  {

                  echo("this is not a leap year");
                  }

                  ?>[/CODE]


                  i'm not getting any errors, but basicly every year is a leap year :/

                  can you comment on this code, tell me if i'm anywhere close, if i'm on the right track....... i know there's probably a cleaner faster way to do this, but i'm just trying to understand conditionals.

                  basicly, i'm trying to say in the code, if x is divided by 4, 100 or 400, and you get a module with nothing left over, it's a leap year.

                  am i making any sence?
                  this is it:
                  [php]
                  $x = $_POST["year"];

                  if (($x%4) == 0)
                  {
                  echo("this is a leap year");
                  }
                  else
                  {
                  echo("this is not a leap year");
                  }
                  [/php]

                  there is nothing other that that.

                  Comment

                  • ballygowanboy
                    New Member
                    • Jul 2007
                    • 47

                    #10
                    Originally posted by epots9
                    this is it:
                    [php]
                    $x = $_POST["year"];

                    if (($x%4) == 0)
                    {
                    echo("this is a leap year");
                    }
                    else
                    {
                    echo("this is not a leap year");
                    }
                    [/php]

                    there is nothing other that that.


                    ok, a little help from a guy in work, and here it is!
                    [CODE=php]

                    <?php

                    $x = $_POST["year"];


                    $leap = $x;


                    if ( ( ($x % 4 == 0) && ($x % 100 !=0) ) || ($x % 400 == 0) )

                    {
                    echo("this is a leap year");
                    }


                    else
                    {

                    echo("this is not a leap year");
                    }

                    ?>[/CODE]


                    my brain's still a bit fuzzy over the whole leap year thing, but i'm starting to get the whole conditional thing, oh, and i know how modules work!

                    Comment

                    • epots9
                      Recognized Expert Top Contributor
                      • May 2007
                      • 1352

                      #11
                      inside your if, y don't u just use '%4'? y do u need '%100' or '%400'?

                      if it returns 0 its a leap year, if it returns anything else its not a leap year.

                      Comment

                      • ballygowanboy
                        New Member
                        • Jul 2007
                        • 47

                        #12
                        Originally posted by epots9
                        inside your if, y don't u just use '%4'? y do u need '%100' or '%400'?

                        if it returns 0 its a leap year, if it returns anything else its not a leap year.

                        ye, you need the other 2, as years ending in 00 is not a leap year, except for every 400 years.

                        Comment

                        • mrhoo
                          Contributor
                          • Jun 2006
                          • 428

                          #13
                          If a year is evenly divisibly by 100 it is not a leap year, unless it is also evenly divisibly by 400. 1900 was not a leap year, 2000 was.

                          Comment

                          Working...