Files
freeCodeCamp/curriculum/challenges/japanese/10-coding-interview-prep/project-euler/problem-19-counting-sundays.md
2022-01-20 20:30:18 +01:00

1.8 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5900f37f1000cf542c50fe92 問題 19: 日曜日を数え上げる 5 301827 problem-19-counting-sundays

--description--

次の情報が与えられていますが、自分で調べても構いません。

  • 1900 年 1 月 1 日は月曜日であった。
  • 9 月、
    4 月、6 月、11 月は 30 日あり、
    残りの月は 31 日ある。
    2 月だけは例外で、
    28 日あり、
    うるう年には 29 日ある。
  • 4 で割り切れる年はうるう年だが、400 で割り切れない 100 の倍数年はうるう年ではない。

20 世紀 (1901 年 1 月 1 日〜2000 年 12 月 31 日) の間に月の 1 日目が日曜日であったのは何回ですか。

--hints--

countingSundays(1943, 1946) は数値を返す必要があります。

assert(typeof countingSundays(1943, 1946) === 'number');

countingSundays(1943, 1946) は 6 を返す必要があります。

assert.strictEqual(countingSundays(1943, 1946), 6);

countingSundays(1995, 2000) は 10 を返す必要があります。

assert.strictEqual(countingSundays(1995, 2000), 10);

countingSundays(1901, 2000) は 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;
}