<!DOCTYPE html>
<script src="leap1.js" type="text/Javascript">
</script>
<body>
<h1> Leap year or Not? </h1>
<form method="get">
<p>
<label for="yr"> Enter the Year: </label>
<input type="text" id="yr" name="yr" />
</p>
<input type="button" value="Find out here!"
onclick="is_leap();" />
</form>
</body>
</html>
and here is the javascript:
Code:
function is_leap()
{
var form = btn.form;
var yr=document.getElementById('yr').value;
if (yr%4 == 0)
{
if (yr%100 == 0)
{
if (yr%400 != 0)
{
alert("Not Leap");
return "false";
}
if (yr%400 == 0)
{
alert("Leap");
return "true";
}
}
if (yr%100 != 0)
{
alert("Leap");
return "true";
}
}
if (yr%4 != 0)
{
alert("Not Leap");
return "false";
}
}
I don't understand why there is not alert popping up. I had this program the other day:
Code:
function isleap()
{
var form = btn.form;
var year = Number(form.year.value);
{
{ if ( 0 ==yr % 4 && 0 != yr % 100 || 0 == yr % 400);
if (1582 < year)
alert('' + yr + ' is a leap year.');
return;
}
{ if (1582> yr);
alert('Gregorian Calendar not made yet');
return;
}
}
}
which randomly decided to work and refused to work again.
Last edited by logepoge1; 10-19-2012 at 01:33 AM..
Reason: Wrong code tag
And by the way, there's an easier way to find out if the year is a leap year:
Code:
function isLeapYear( year )
{
var dt = new Date( year, 1, 29 ); // Feb 29th of given year
return ( dt.getDate() == 29 );
}
If the year is NOT a leap year, JavaScript will automatically convert Feb 29th to March 1st for you, so getDate() will then return 1 instead of 29.
Of course, JavaScript doesn't take into account pre-Gregorian years, but do you REALLY care about them???
Besides, when the Gregorian Calendar took effect varies from country to country (and in some countries, even by region to region). There were a few regions, including all of Russia and most of Eastern Europe that didn't adopt the Gregorian calendar until the 1900s, Even in the USA, it wasn't used in Alaska until 1867.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 10-19-2012 at 02:41 AM..
Did you know that George Washington was born on January 11th? That's because the provinces, as possessions of Great Britain, were still on the Julian Calendar when he was born. He adjusted his birth date later in life to reflect what it WOULD have been if the Gregorian Calendar had been in effect at the time. Oh, the trivia we learn in high school and carry with us for over 60 years.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
function is_leap()
{
var yr=document.getElementById('yr').value;
if(yr<1583)
{
alert("Enter a year after 1582.");
return false;
}
if (yr%4 == 0)
{
if (yr%100 == 0)
{
if (yr%400 != 0)
{
alert(""+ yr + " is not a leap year");
return "false";
}
else
{
alert(""+ yr + " is a leap year");
return "true";
}
}
else
{
alert(""+ yr +" is a leap year");
return "true";
}
}
else
{
alert(""+ yr +" is not a leap year");
return "false";
}
}
And here is the html:
Code:
<!DOCTYPE html>
<script src="leap1.js" type="text/Javascript">
</script>
<body>
<h1> Leap year or Not? </h1>
<form name="leapyear" method="get">
<p>
<label for="yr"> Enter the Year: </label>
<input type="text" id="yr" name="yr" />
</p>
<input type="submit" value="Find out here!"
onclick="is_leap();" />
</form>
</body>
</html>
What seemed to fix it was on the html. I had onclick="button". It worked when I changed button to submit. I am working on the CSS now and the HTML5 to make it look better. Oh and the 1582 was just to make it more accurate. Thanks everyone.
And by the way, there's an easier way to find out if the year is a leap year:
Code:
function isLeapYear( year )
{
var dt = new Date( year, 1, 29 ); // Feb 29th of given year
return ( dt.getDate() == 29 );
}
If the year is NOT a leap year, JavaScript will automatically convert Feb 29th to March 1st for you, so getDate() will then return 1 instead of 29.
That's definitely the best way to test if it is a leap year. You don't even need to know the leap year rules in order to test that way (of course that probably doesn't apply leap year rule 3 but then few people would currently care that 4882 and 8182 are going to be leap years and there's plenty of time to change the way the JavaScript Date object works to take it into account before it does become significant)
Quote:
Originally Posted by Old Pedant
Even in the USA, it wasn't used in Alaska until 1867.
Isn't that because Alaska was part of Russia before then and only became part of the USA in that year (it also swapped from one side of the international date line to the other at the same time as switching calendars).
// Also useful to determine the last day of a Month (the day 0 of the following month)
function isLeap2(year){return new Date(year,2,0).getDate()==29;}