find the leap year by using for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manku
    New Member
    • Jan 2015
    • 1

    find the leap year by using for loop

    enter any year than display the leap or not
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    You don't need for loop to determine if leap year or not. Try this code I made.
    Code:
    <html>
    <head>
    <title>Zick Sample</title>
    <script>
    function leapyear()
    {
    	var year = document.getElementById("inputyear").value;
    	if(year != "")
    	{
    		if ((year % 4 == 0) && year % 100 != 0)
    	    {
    	        alert(year + " is a leap year.");
    	    }
    	    else if (year % 400 == 0)
    	    {
    	        alert(year + " is a leap year.");
    	    }
    	    else
    	    {
    	        alert(year + " is not a leap year.");
    	    }
    	}
    }
    </script>
    </head>
    <body>
    <form method="post" action="javascript:;">
    	<input type="text" id="inputyear" placeholder="Enter the year."><br>
    	<input type="submit" value="Submit" onclick="leapyear()">
    </form>
    </body>
    </html>

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      one of the best leap year calculations I found:
      Code:
      (new Date(year, 1, 29).getDate() === 29)

      Comment

      Working...