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.119 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.118 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.117 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.116 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.115 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.114 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.113 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.111 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.110 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.109 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.108 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.107 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.106 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.105 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.104 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.103 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.102 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.101 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.100 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.99 tracks/javascript/exercises/meetup/example.js