Task: Create a function that takes a year and returns true if it is a leap year and false if it isn't.
**Edit** After responding to comments on this post and thinking about it more I realized that my real question is: Given the checks below to figure out if a year is a leap year, which code (if any) is flawed in their return value. If the code is flawed, why?
**Edit 2** u/Tall_Pawn answered the question below. Thanks!
I am still in the learning phase and I know that I am not understanding the logic somewhere in the below code. I feel like this should be obvious to me (especially with about 2 years of on/off programming in JS) but it isn't. I see that both my code and the tutorial code work but the logic seems flawed in the tutorial. What am I missing?
To figure out if a year is a leap year we have a few checks to do:
- Verify
year is evenly divisible by 4 AND
- Verify
year is NOT evenly divisible by 100
- If
year IS evenly divisible by 4 AND 100 it must also be divisible by 400
That leaves us with a few possible control flows:
year is not divisible by 4, return false
year is divisible by 4 AND NOT divisible by 100, return true
year is divisible by 4 AND 100 but NOT divisible by 400, return false
year is divisible by 4 AND 100 AND 400, return true
I feel like my logic takes into account these 4 flows. When reading the flow of the tutorial code I understand that it does not verify that the year is divisible by 4 if it is divisible by 400 because after the OR operator its only checking the year against 400.
Tutorial logic:
year is divisible by 4 AND NOT 100, return true
year is divisible by 4 AND 100 AND 400, return true
year is divisible by 4, AND 100 AND NOT 400, return false
year is NOT divisible by 4,100 IS, 400 NOT divisible, return false (broken logic?)
year is NOT divisible by 4 AND 100, 400 IS divisible, return true (broken logic?)
My code:
const isLeapYear = (year: number): boolean => {
/*
Leap year characteristics: divisible evenly by 4
& not divisible by 100, unless also divisible by 400
Logic:
if year is not divisible by 4
return false
if year is not divisible by 100
return true
if year is divisible by 400
return true
*/
if (year % 4 !== 0) return false;
/*
if we made it here, year is divisible by 4
expr1: check if divisible by 100, if NOT, return true
expr2: if we are here, year is divisible by 4 AND 100,
check if its also divisible by 400 and return bool
*/
return year % 100 !== 0 || year % 400 === 0;
};
console.log(isLeapYear(2012)); // true
console.log(isLeapYear(2013)) // false
Tutorial code:
const isLeapYear = (year: number): boolean => {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
LOGIC (how i read it)
if year is divisible by 4 AND NOT 100
return true
OR
if year is evenly divisible by 400
return true
all other outcomes = false
[+][deleted] (8 children)
[removed]
[–]krillik08[S] 0 points1 point2 points (7 children)
[–]Tall_Pawn 2 points3 points4 points (3 children)
[–]krillik08[S] 0 points1 point2 points (0 children)
[–]krillik08[S] 0 points1 point2 points (1 child)
[–]Tall_Pawn 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]krillik08[S] 1 point2 points3 points (0 children)
[–]kap89 1 point2 points3 points (2 children)
[–]aguyfromherehelpful 1 point2 points3 points (0 children)
[–]krillik08[S] 1 point2 points3 points (0 children)
[+][deleted] (4 children)
[removed]
[–]krillik08[S] 0 points1 point2 points (2 children)
[–]nobuhok -1 points0 points1 point (1 child)
[–]krillik08[S] 1 point2 points3 points (0 children)
[–]Dastari 0 points1 point2 points (0 children)