()
| 19 | import { isLeapYear } from '../Maths/LeapYear' |
| 20 | |
| 21 | function problem19() { |
| 22 | let sundaysCount = 0 // Count of Sundays |
| 23 | let dayOfWeek = 2 // 1st Jan 1900 was a Monday, so 1st Jan 1901 was a Tuesday |
| 24 | |
| 25 | for (let year = 1901; year <= 2000; year++) { |
| 26 | for (let month = 1; month <= 12; month++) { |
| 27 | if (dayOfWeek === 0) { |
| 28 | // If it's a Sunday (0 is Sunday, 1 is Monday, ..., 6 is Saturday) |
| 29 | sundaysCount++ |
| 30 | } |
| 31 | |
| 32 | let daysInMonth = 31 |
| 33 | if (month === 4 || month === 6 || month === 9 || month === 11) { |
| 34 | // April, June, September, November |
| 35 | daysInMonth = 30 |
| 36 | } else if (month === 2) { |
| 37 | // February |
| 38 | daysInMonth = isLeapYear(year) ? 29 : 28 |
| 39 | } |
| 40 | |
| 41 | dayOfWeek = (dayOfWeek + daysInMonth) % 7 // Calculate the day of the week |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return sundaysCount |
| 46 | } |
| 47 | |
| 48 | export { problem19 } |
no test coverage detected