Hello everyone,
I wrote a program to calculate leap years to practice JavaScript statements using if…else. But now I’m confused because I do not know how I can solve it for myself!
I wrote it in two ways and both ways I got the right answer. The first way is simple, but the second way I have no explanation about it and I am confused about it. Especially in the Truthy and Falsy part !!! it is kind of unlike calculation !
if ( !(year % 4) ){
if (!(year % 100)){
if ( !(year % 400)){
I would appreciate it if you guide me to understand it well.
/*----------------- Leap Year -------------- */
function isLeap (year) {
if (year % 4 === 0) {
if (year % 100 === 0){
if (year % 400 === 0){
The year ${year} IS a leap year!!
;
} else {
return The year ${year} is NOT a leap year!!
;
}
} else {
return The year ${year} IS a leap year!!
;
}
}else {
return The year ${year} is NOT a leap year!!
;
}
}
/*----------------- SECOND WAY -------------- */
const isLeap = year => {
if ( !(year % 4) ){
if (!(year % 100)){
if ( !(year % 400)){
console.log(The year ${year} IS a leap year
);
}else {
console.log(The year ${year} is NOT a leap year
);
}
}else {
console.log(The year ${year} IS a leap year
);
}
}else {
console.log(The year ${year} is NOT a leap year
);
}
};
isLeap(2024);
/*----------------- Leap Year End -------------- */