2.4 KiB
2.4 KiB
id, challengeType, title, forumTopicId, localeTitle
id | challengeType | title | forumTopicId | localeTitle |
---|---|---|---|---|
5900f37f1000cf542c50fe92 | 5 | Problem 19: Counting Sundays | 301827 | Задача 19: подсчет воскресений |
Description
- 1 января 1900 года был понедельник.
- Тридцать дней - сентябрь,
Апреле, июне и ноябре.
Все остальные тридцать один,
Сохраняя только февраль,
Который имеет двадцать восемь, дождь или блеск.
И на високосные годы, двадцать девять. - Високосный год происходит в любой год, равномерно делимый на 4, но не на столетие, если он не делится на 400. Сколько воскресений упало в первый месяц месяца в двадцатом веке (1 января 1901 года по 31 декабря 2000 года)?
Instructions
Tests
tests:
- text: <code>countingSundays(1943, 1946)</code> should return 6.
testString: assert.strictEqual(countingSundays(1943, 1946), 6);
- text: <code>countingSundays(1995, 2000)</code> should return 10.
testString: assert.strictEqual(countingSundays(1995, 2000), 10);
- text: <code>countingSundays(1901, 2000)</code> should return 171.
testString: assert.strictEqual(countingSundays(1901, 2000), 171);
Challenge Seed
function countingSundays(firstYear, lastYear) {
// Good luck!
return true;
}
countingSundays(1943, 1946);
Solution
function countingSundays(firstYear, lastYear) {
let sundays = 0;
for (let year = firstYear; year <= lastYear; year++) {
for (let month = 0; month <= 11; month++) {
const thisDate = new Date(year, month, 1);
if (thisDate.getDay() === 0) {
sundays++;
}
}
}
return sundays;
}