Design and implement an application that reads an interger value rpresenting a y
ID: 3622673 • Letter: D
Question
Design and implement an application that reads an interger value rpresenting a year from the user. The purpose of the program is to determine if the year is a leap year if it is divisble by 4, unless it is also divisble by 100 but not 400. For example, the year 2003 is not a leap year, but 2004 is. The year 1900 is not a leap year becuase it it divisible by 100, but the year 2000 is a leap year because even though it is divisble by 100, it is also divisible by 400. Produce an error message for any input value less than 1582 (the year the Gregorian calendar was adopted).
Then modify the solution to programming so that the user can evaluate multiple years. Allow the user to terminate the program using an appropriate sentinel value. Validate each input value to ensure is its greater than or equal to 1582
Java Script, 1st semester
Explanation / Answer
// save as leap.html and run.
<script type="text/javascript">
function checkleapyear(datea)
{
datea = parseInt(datea);
if(datea%4 == 0)
{
if(datea%100 != 0)
{
return true;
}
else
{
if(datea%400 == 0)
return true;
else
return false;
}
}
return false;
}
function isLeapYear()
{
var df = document.sd.yv.value;
if(parseInt(df)<1582)
{
alert("Invalid Year ");
document.sd.val.value="";
return;
}
if(df!= "" && parseInt(df))
{
var res = checkleapyear(df);
if(res)
{
document.sd.val.style.color = "green";
document.sd.val.value = df+" is a leap year";
}
else
{
document.sd.val.style.color = "#9a235d";
document.sd.val.value = df+" is not a leap year";
document.sd.val.value = df+" is not a leap year";
}
}
else
{
alert("Enter a proper value");
}
return false;
}
function isInteger(s)
{
var i;
s = s.toString();
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (isNaN(c))
{
alert("Given value is not a number");
return false;
}
}
return true;
}
</script>
<form name=sd method=post action="" >
Enter a year [ ex: 2020 ] - <input name=yv type=text><input type="button" value=go><br>
<input type=text name=val size=30 readonly>
</form>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.