-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path019.js
56 lines (47 loc) · 1.44 KB
/
019.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Problem 19: Counting Sundays
//Storing days of the month in an array, except Feb
const daysInMonth = [0,31,0,31,30,31,30,31,31,30,31,30,31];
//Function for days in Feb due to leap years
function daysInFeb(year) {
if (year % 4 != 0)
return 28;
if (year % 100 == 0 && year % 400 != 0)
return 28;
return 29;
}
function countingSundays(firstYear, lastYear) {
//Counting the number of days from Dec 31 1899 (Sunday)
//A Sunday is every 7 days, so checking if number of days is perfectly divisible by 7
//Since Jan 1, 1900 is a Monday, 1 day back to make it a sunday
var days = 1;
//Total number of Sundays
var total = 0;
//The current year
var year = 1900;
//Number of days from 1900 to Jan 1st of the first year to be considered
while (year != firstYear)
{
for (var month = 1; month < 13; month++)
{
days += daysInMonth[month];
if (month == 2)
days += daysInFeb(year);
}
year++;
}
//Calculating number of days and dividing after each month by 7
while (year != lastYear + 1)
{
for (month = 1; month < 13; month++)
{
if (days % 7 == 0) //Sunday
total++;
days += daysInMonth[month];
if (month == 2)
days += daysInFeb(year);
}
year++;
}
return total;
}
console.log(countingSundays(1943, 1946));