Find the last Sunday of each month: Difference between revisions

m (formatting of task description)
Line 1,440:
 
<lang JavaScript>(function () {
'use strict';
 
// lastSundaysOfYear :: Int -> [Date]
function lastSundaysOfYear(y) {
return lastDaysOfYear(y)[
));31,
0 === y % 4 && 0 !== y % 100 || 0 === y % 400 ? 29 : 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
})]
.map(function (d, m) {
var dte = new Date(Date.UTC(y, m, d, 0, 0, 0, 0));
y, m, d, 0, 0, 0, 0
));
 
return new Date(Date.UTC(
y, m, dte.getDate() - dte.getDay(), 0, 0, 0, 0
dte.getDate() - dte.getDay(),
0, 0, 0, 0
));
});
}
 
// lastDaysOfYear :: Int -> [Int]
function lastDaysOfYear(y) {
return [
31,
isLeapYear(y) ? 29 : 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
];
}
 
// isLeapYear :: Int -> Bool
function isLeapYear(y) {
return ((y % 4 === 0) && (y % 100 !== 0)) ||
(y % 400 === 0);
}
 
 
 
// TESTING
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
function zipWith(f, xs, ys) {
return xs.length === ys.length ? (
xs.map(function (x, i) {
return f(x, ys[i]);
})
) : undefined;
}
 
// TESTINGTEST
 
// 2013Results andfor 20162013, side2016 by(leap sideyear)
// displayed in parallel
return zipWith(
return function zipWith(f, xs, ys) {
return xs.length === ys.length ? (xs
xs .map(function (x, i) {
dte.getDatereturn f()x, - dte.getDay(ys[i]),;
}) : 0, 0, 0, 0undefined;
return [}(
function (d1, d2) {
return [d1, d2].map(function (dte) {
.map(function (dte) {
return dte.toISOString()
.substr(0, 10);
})
.join('"\t'");
},
lastSundaysOfYear(2013),
lastSundaysOfYear(2016)
)
.join('"\n'");
})();</lang>
 
9,655

edits