Sha256: bdd1a3b35dab74ce2198ee5ca59058bdd570d3daede2ab379befbe02e2edbd35

Contents?: true

Size: 1.35 KB

Versions: 133

Compression:

Stored size: 1.35 KB

Contents

'use strict';

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

function meetupDay(year, month, dayOfWeek, which) {
  var candidates = _getCandidates(year, month, dayOfWeek);
  var res;
  var day;

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

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

  return res;
}

function _getCandidates(year, month, dayOfWeek) {
  var d;
  var i;
  var numDaysInMonth = new Date(year, month + 1, 0).getDate();
  var 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) {
  var daysInd = {
    'sunday': 0,
    'monday': 1,
    'tuesday': 2,
    'wednesday': 3,
    'thursday': 4,
    'friday': 5,
    'saturday': 6
  };
  return daysInd[day.toLowerCase()];
}

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

module.exports = meetupDay;

Version data entries

133 entries across 133 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.97 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.96 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.95 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.94 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.93 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.92 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.91 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.90 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.89 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.88 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.87 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.86 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.85 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.84 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.83 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.82 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.81 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.80 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.79 tracks/javascript/exercises/meetup/example.js