Files
freeCodeCamp/curriculum/challenges/spanish/08-coding-interview-prep/project-euler/problem-19-counting-sundays.spanish.md
2018-10-08 13:51:51 -04:00

2.1 KiB

id, localeTitle, challengeType, title
id localeTitle challengeType title
5 5900f37f1000cf542c50fe92 5 Problem 19: Counting Sundays

Description

Se le proporciona la siguiente información, pero es posible que prefiera hacer una investigación por sí mismo.
  • El 1 de enero de 1900 fue un lunes.
  • Treinta días tiene septiembre,
    Abril, junio y noviembre.
    Todos los demás tienen treinta y uno,
    Salvando febrero solo,
    Que tiene veintiocho, llueva o truene.
    Y en años bisiestos, veintinueve.
  • Un año bisiesto ocurre en cualquier año divisible por 4, pero no en un siglo a menos que sea divisible por 400.
  • ¿Cuántos domingos cayeron el primer día del mes durante el siglo veinte (1 de enero de 1901 a 31 de diciembre de 2000)?

Instructions

Tests

tests:
  - text: &#39; <code>countingSundays(1943, 1946)</code> debe devolver 6.&#39;
    testString: 'assert.strictEqual(countingSundays(1943, 1946), 6, "<code>countingSundays(1943, 1946)</code> should return 6.");'
  - text: &#39; <code>countingSundays(1995, 2000)</code> debe devolver 9.&#39;
    testString: 'assert.strictEqual(countingSundays(1995, 2000), 9, "<code>countingSundays(1995, 2000)</code> should return 9.");'
  - text: &#39; <code>countingSundays(1901, 2000)</code> debe devolver 171.&#39;
    testString: 'assert.strictEqual(countingSundays(1901, 2000), 171, "<code>countingSundays(1901, 2000)</code> should return 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 = 1; month <= 12; month++) {
      const thisDate = new Date(year, month, 1);
      if (thisDate.getDay() === 0) {
        sundays++;
      }
    }
  }
  return sundays;
}