By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

find the leap year by using for loop

P: 1
enter any year than display the leap or not
Jan 19 '15 #1
Share this Question
Share on Google+
2 Replies


Exequiel
100+
P: 287
You don't need for loop to determine if leap year or not. Try this code I made.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Zick Sample</title>
  4. <script>
  5. function leapyear()
  6. {
  7.     var year = document.getElementById("inputyear").value;
  8.     if(year != "")
  9.     {
  10.         if ((year % 4 == 0) && year % 100 != 0)
  11.         {
  12.             alert(year + " is a leap year.");
  13.         }
  14.         else if (year % 400 == 0)
  15.         {
  16.             alert(year + " is a leap year.");
  17.         }
  18.         else
  19.         {
  20.             alert(year + " is not a leap year.");
  21.         }
  22.     }
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <form method="post" action="javascript:;">
  28.     <input type="text" id="inputyear" placeholder="Enter the year."><br>
  29.     <input type="submit" value="Submit" onclick="leapyear()">
  30. </form>
  31. </body>
  32. </html>
  33.  
Jan 20 '15 #2

Dormilich
Expert Mod 5K+
P: 8,639
one of the best leap year calculations I found:
Expand|Select|Wrap|Line Numbers
  1. (new Date(year, 1, 29).getDate() === 29)
Jan 20 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.