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.180 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.179 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.178 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.177 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.176 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.175 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.174 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.173 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.172 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.171 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.170 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.169 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.167 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.166 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.165 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.164 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.163 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.162 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.161 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.160 tracks/javascript/exercises/meetup/example.js