topic description
write an expression that results in whether 2018 is a leap year.
conditions for judging leap years:
is a multiple of 400, or a multiple of 4 and not a multiple of 100.
1900 false
2000 true
2004 true
ideas
first judge whether it is a number by if. When it is a number, the nested if judges the condition of leap year
if it is not a number, then output "input is not a number"
problems encountered
if (typeof year != "number") {
return console.log("");
}
determine the type of year through typeof, but if you enter a non-numeric number, you will directly report an error "xxx is not defined"
, but just add "" to the input box
overall code
function isLeapYear(year) {
if (typeof year != "number") {
return console.log("");
} else {
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
return console.log(year + "");
} else {
return console.log(year + "");
}
}
}
isLeapYear(asd);
expect to solve doubts
1. Why did this happen? what is the reason for it?
2. How to get the desired effect without quotation marks when calling (prompt "input is not a number")