JavaScript Implementation of Problem 19
View source code here on GitHub!
- p0019()
Project Euler Problem 19
This one ended up being very easy thanks to the Date object
Problem:
You are given the following information, but you may prefer to do some research for yourself.
1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
- Returns:
number --
1/**
2 * Project Euler Problem 19
3 *
4 * This one ended up being very easy thanks to the Date object
5 *
6 * Problem:
7 *
8 * You are given the following information, but you may prefer to do some research
9 * for yourself.
10 *
11 * 1 Jan 1900 was a Monday.
12 * Thirty days has September,
13 * April, June and November.
14 * All the rest have thirty-one,
15 * Saving February alone,
16 * Which has twenty-eight, rain or shine.
17 * And on leap years, twenty-nine.
18 * A leap year occurs on any year evenly divisible by 4, but not on a century
19 * unless it is divisible by 400.
20 *
21 * How many Sundays fell on the first of the month during the twentieth century
22 * (1 Jan 1901 to 31 Dec 2000)?
23 *
24 * @return {number}
25 */
26exports.p0019 = function() {
27 let answer = 0;
28 for (let x = 1901; x < 2001; x += 1) {
29 for (let y = 1; y < 13; y += 1) {
30 if (new Date(x, y, 1).getDay() == 0) {
31 answer += 1;
32 }
33 }
34 }
35 return answer;
36};