This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,45 @@
function startsOnFriday(month, year)
{
// 0 is Sunday, 1 is Monday, ... 5 is Friday, 6 is Saturday
return new Date(year, month, 1).getDay() === 5;
}
function has31Days(month, year)
{
return new Date(year, month, 31).getDate() === 31;
}
function checkMonths(year)
{
var month, count = 0;
for (month = 0; month < 12; month += 1)
{
if (startsOnFriday(month, year) && has31Days(month, year))
{
count += 1;
document.write(year + ' ' + month + '<br>');
}
}
return count;
}
function fiveWeekends()
{
var
startYear = 1900,
endYear = 2100,
year,
monthTotal = 0,
yearsWithoutFiveWeekends = [],
total = 0;
for (year = startYear; year <= endYear; year += 1)
{
monthTotal = checkMonths(year);
total += monthTotal;
// extra credit
if (monthTotal === 0)
yearsWithoutFiveWeekends.push(year);
}
document.write('Total number of months: ' + total + '<br>');
document.write('<br>');
document.write(yearsWithoutFiveWeekends + '<br>');
document.write('Years with no five-weekend months: ' + yearsWithoutFiveWeekends.length + '<br>');
}
fiveWeekends();

View file

@ -0,0 +1,61 @@
var Months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
];
var leap = 0,
// Relative offsets between first day of each month
offset = [3,0,3,2,3,2,3,3,2,3,2,3],
// Months that contain 31 days
longMonths = [1,3,5,7,8,10,12],
startYear = 1900,
year = startYear,
endYear = 2100,
// Jan 1, 1900 starts on a Monday
day = 1,
totalPerYear = 0,
total = 0,
without = 0;
for (; year < endYear + 1; year++) {
leap = totalPerYear = 0;
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
leap = 1;
}
} else {
leap = 1;
}
}
for (var i = 0; i < offset.length; i++) {
for (var j = 0; day === 5 && j < longMonths.length; j++) {
if (i + 1 === longMonths[j]) {
console.log(year + '-' + Months[i]);
totalPerYear++;
total++;
break;
}
}
// February -- if leap year, then +1 day
if (i == 1) {
day = (day + leap) % 7;
} else {
day = (day + offset[i]) % 7;
}
}
if (totalPerYear === 0) {
without++;
}
}
console.log('Number of months that have five full weekends from 1900 to 2100: ' + total);
console.log('Number of years without any five full weekend months: ' + without);