Avatar of freddie2025

freddie2025's solution

to Meetup in the JavaScript Track

Published at Mar 31 2020 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Calculate the date of meetups.

Typically meetups happen on the same day of the week. In this exercise, you will take a description of a meetup date, and return the actual meetup date.

Examples of general descriptions are:

  • The first Monday of January 2017
  • The third Tuesday of January 2017
  • The wednesteenth of January 2017
  • The last Thursday of January 2017

The descriptors you are expected to parse are: first, second, third, fourth, fifth, last, monteenth, tuesteenth, wednesteenth, thursteenth, friteenth, saturteenth, sunteenth

Note that "monteenth", "tuesteenth", etc are all made up words. There was a meetup whose members realized that there are exactly 7 numbered days in a month that end in '-teenth'. Therefore, one is guaranteed that each day of the week (Monday, Tuesday, ...) will have exactly one date that is named with '-teenth' in every month.

Given examples of a meetup dates, each containing a month, day, year, and descriptor calculate the date of the actual meetup. For example, if given "The first Monday of January 2017", the correct meetup date is 2017/1/2.

Setup

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Requirements

Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.

Source

Jeremy Hinegardner mentioned a Boulder meetup that happens on the Wednesteenth of every month https://twitter.com/copiousfreetime

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

meetup.spec.js

import { meetupDay } from './meetup';

describe('meetupDay()', () => {
  test('test monteenth of may 2013', () => {
    expect(meetupDay(2013, 4, 'Monday', 'teenth')).toEqual(new Date(2013, 4, 13));
  });

  xtest('test saturteenth of february 2013', () => {
    expect(meetupDay(2013, 1, 'Saturday', 'teenth')).toEqual(new Date(2013, 1, 16));
  });

  xtest('test first tuesday of may 2013', () => {
    expect(meetupDay(2013, 4, 'Tuesday', '1st')).toEqual(new Date(2013, 4, 7));
  });

  xtest('test second monday of april 2013', () => {
    expect(meetupDay(2013, 3, 'Monday', '2nd')).toEqual(new Date(2013, 3, 8));
  });

  xtest('test third thursday of september 2013', () => {
    expect(meetupDay(2013, 8, 'Thursday', '3rd')).toEqual(new Date(2013, 8, 19));
  });

  xtest('test fourth sunday of march 2013', () => {
    expect(meetupDay(2013, 2, 'Sunday', '4th')).toEqual(new Date(2013, 2, 24));
  });

  xtest('test last thursday of october 2013', () => {
    expect(meetupDay(2013, 9, 'Thursday', 'last')).toEqual(new Date(2013, 9, 31));
  });

  xtest('test last wednesday of february 2012', () => {
    expect(meetupDay(2012, 1, 'Wednesday', 'last')).toEqual(new Date(2012, 1, 29));
  });

  xtest('test last wednesday of december 2014', () => {
    expect(meetupDay(2014, 11, 'Wednesday', 'last')).toEqual(new Date(2014, 11, 31));
  });

  xtest('test last sunday of only four week february 2015', () => {
    expect(meetupDay(2015, 1, 'Sunday', 'last')).toEqual(new Date(2015, 1, 22));
  });

  xtest('test first friday of december 2012', () => {
    expect(meetupDay(2012, 11, 'Friday', '1st')).toEqual(new Date(2012, 11, 7));
  });

  xtest('test fifth monday of march 2015', () => {
    expect(meetupDay(2015, 2, 'Monday', '5th')).toEqual(new Date(2015, 2, 30));
  });

  xtest('test nonexistent fifth monday of february 2015', () => {
    expect(() => {
      meetupDay(2015, 1, 'Monday', '5th');
    }).toThrow();
  });
});
//
// This is only a SKELETON file for the 'Meetup' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

const schedules = {
  'teenth': 13,
  '1st': 1,
  '2nd': 8,
  '3rd': 15,
  '4th': 22,
  '5th': 29,
  'last': 0
}

const daysOfWeek = {
  Sunday: 0,
  Monday: 1,
  Tuesday: 2,
  Wednesday: 3,
  Thursday: 4,
  Friday: 5,
  Saturday: 6
}

export const meetupDay = (year, month, dayOfWeek, schedule) =>  {

  let daysInMonth = new Date(year, month +1, 0).getDate();
  let dayOfMonth = 1;

  if (daysInMonth === 28 && new Date(year, month, 1).getDay() === 0 && schedule === '5th') {
    throw `${month} ${year} does not contain a 5th week.`;
  }

  if(schedule === 'last') {
      for (let day = daysInMonth; day >= 1; day--) {
          if(new Date(year, month, day).getDay() == daysOfWeek[dayOfWeek]) {
              dayOfMonth = day;
              break;
          }
      }
  }
  else {
      for (let day = schedules[schedule]; day <= daysInMonth; day++) {
          if(new Date(year, month, day).getDay() == daysOfWeek[dayOfWeek]) {
              dayOfMonth = day;
              break;
          }
      }
  }
  
  return new Date(year, month, dayOfMonth);
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?