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.159 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.158 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.157 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.156 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.155 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.154 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.153 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.152 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.151 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.150 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.149 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.148 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.147 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.146 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.145 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.144 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.143 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.142 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.141 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.140 tracks/javascript/exercises/meetup/example.js