1.7 KiB
1.7 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5900f37f1000cf542c50fe92 | Problema 19: Contando le domeniche | 1 | 301827 | problem-19-counting-sundays |
--description--
Ti sono fornite le seguenti informazioni, ma potresti voler fare un po' di ricerca per conto tuo.
- Il 1 Gennaio 1900 era un lunedì.
- Trenta giorni a novembre,
con april, giugno e settembre,
di ventotto ce n'è uno,
tutti gli altri ne han trentuno. - Un anno bisestile si verifica su qualsiasi anno divisibile per 4, ma non in un anno divisibile per 100 a meno che non sia divisibile per 400.
Quante domeniche sono cadute il primo del mese durante il ventesimo secolo (dal 1 Gen 1901 al 31 Dic 2000)?
--hints--
countingSundays(1943, 1946)
dovrebbe restituire un numero.
assert(typeof countingSundays(1943, 1946) === 'number');
countingSundays(1943, 1946)
dovrebbe restituire 6.
assert.strictEqual(countingSundays(1943, 1946), 6);
countingSundays(1995, 2000)
dovrebbe restituire 10.
assert.strictEqual(countingSundays(1995, 2000), 10);
countingSundays(1901, 2000)
dovrebbe restituire 171.
assert.strictEqual(countingSundays(1901, 2000), 171);
--seed--
--seed-contents--
function countingSundays(firstYear, lastYear) {
return true;
}
countingSundays(1943, 1946);
--solutions--
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;
}