MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / problem19

Function problem19

Project-Euler/Problem019.js:21–46  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19import { isLeapYear } from '../Maths/LeapYear'
20
21function 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
48export { problem19 }

Callers 1

Problem019.test.jsFile · 0.90

Calls 1

isLeapYearFunction · 0.90

Tested by

no test coverage detected