Sha256: e329f8ad7fb8de127891b95fc711d3335a6b9ee62dc7101977b5bcb72dc1034c

Contents?: true

Size: 1.3 KB

Versions: 30

Compression:

Stored size: 1.3 KB

Contents

export default function meetupDay (year, month, dayOfWeek, which) {

  let candidates = getCandidates(year, month, dayOfWeek);
  let res;

  which = which.toLowerCase();

  if (which === 'teenth') {
    res = find(candidates, d =>  {
      return 13 <= d.getDate() && d.getDate() <= 19;
    });
  } else if (which === 'last') {
    res = candidates.pop();
  } else {
    which = parseInt(which) - 1;
    res = candidates.slice(which, which + 1).pop();
  }

  if (!res) { throw new MeetupDayException('Day not found!'); }

  return res;
}

function getCandidates (year, month, dayOfWeek) {
  let d,
    i,
    numDaysInMonth = new Date(year, month + 1, 0).getDate(),
    res = [];

  for (i = 0; i < numDaysInMonth; i++) {
    d = new Date(year, month, i + 1);

    if (d.getDay() === getDayIndex(dayOfWeek)) {
      res.push(d);
    }
  }

  return res;
}

function getDayIndex (day) {
  const daysInd = {
    'sunday': 0,
    'monday': 1,
    'tuesday': 2,
    'wednesday': 3,
    'thursday': 4,
    'friday': 5,
    'saturday': 6
  };

  day = day.toLowerCase();

  return daysInd[day];
}

function find(ary, callback) {
  for (let i = 0; i < ary.length; i++) {
    if (callback(ary[i], i, ary)) { return ary[i]; }
  }
}

function MeetupDayException(message) {
  this.message = message;
  this.name = 'MeetupDayException';
}

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/meetup/example.js