Merry

Finished
1//
2// This is only a SKELETON file for the 'Meetup' exercise. It's been provided as a
3// convenience to get you started writing code faster.
4//
1const DAYS_OF_WEEK = [
2 'Sunday',
3 'Monday',
4 'Tuesday',
5 'Wednesday',
6 'Thursday',
7 'Friday',
8 'Saturday'
9];
10
11const WEEK_IDENTIFIERS = [
12 'first',
13 'second',
14 'third',
15 'fourth',
16 'last',
17 'teenth'
18];
519
620/**
7 * @param {number} year
8 * @param {number} month
9 * @param {string} week
10 * @param {string} dayOfWeek
11 * @returns {Date}
21 * Calculates the date of a meetup based on year, month, week identifier, and day of week.
22 *
23 * @param {number} year - The year of the meetup
24 * @param {number} month - The month of the meetup (1-12)
25 * @param {string} week - The week identifier ('first', 'second', 'third', 'fourth', 'last', 'teenth')
26 * @param {string} dayOfWeek - The day of week ('Sunday', 'Monday', etc.)
27 * @returns {Date} - The calculated date of the meetup
28 *
29 * @example
30 * meetup(2018, 1, 'first', 'Monday') // Returns Date for January 1, 2018
31 * meetup(1953, 8, 'teenth', 'Saturday') // Returns Date for August 15, 1953
1232 */
1333export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
34 // Edge Case: Invalid month values
35 if (month < 1 || month > 12) {
36 throw new Error('Invalid month');
37 }
38
39 // Edge Case: Invalid week identifier
40 if (!WEEK_IDENTIFIERS.includes(week)) {
41 throw new Error('Invalid week identifier');
42 }
43
44 // Edge Case: Invalid day of week
45 if (!DAYS_OF_WEEK.includes(dayOfWeek)) {
46 throw new Error('Invalid day of week');
47 }
48
49 const targetDayIndex = DAYS_OF_WEEK.indexOf(dayOfWeek);
50
51 // Edge Case: Invalid year (negative values)
52 if (year < 0) {
53 throw new Error('Invalid year');
54 }
55
56 if (week === 'teenth') {
57 // For teenth, we check days 13-19
58 for (let day = 13; day <= 19; day++) {
59 const date = new Date(year, month - 1, day);
60 if (date.getDay() === targetDayIndex) {
61 return date;
62 }
63 }
64 } else if (week === 'last') {
65 // For last, we start from the last day of the month and work backwards
66 const lastDay = new Date(year, month, 0).getDate();
67 for (let day = lastDay; day >= 1; day--) {
68 const date = new Date(year, month - 1, day);
69 if (date.getDay() === targetDayIndex) {
70 return date;
71 }
72 }
73 } else {
74 // For first, second, third, fourth
75 const weekIndex = ['first', 'second', 'third', 'fourth'].indexOf(week);
76
77 // Find the first occurrence of the target day
78 const firstDayOfMonth = new Date(year, month - 1, 1);
79 let firstOccurrence = 1 + (targetDayIndex - firstDayOfMonth.getDay() + 7) % 7;
80
81 // Calculate the target date
82 const targetDate = firstOccurrence + weekIndex * 7;
83
84 // Edge Case: Requested week does not exist in the month
85 const daysInMonth = new Date(year, month, 0).getDate();
86 if (targetDate > daysInMonth) {
87 throw new Error('Requested week does not exist in the month');
88 }
89
90 return new Date(year, month - 1, targetDate);
91 }
92
93 // This should never be reached with valid inputs
94 throw new Error('Could not determine meetup date');
1595};
96
97// Handled Edge Cases: Invalid month values, Invalid week identifier, Invalid day of week, Invalid year (negative values), Requested week does not exist in the month
Test NameStatus
monteenth of May 2013
Pass
monteenth of August 2013
Pass
monteenth of September 2013
Pass
tuesteenth of March 2013
Pass
tuesteenth of April 2013
Pass
tuesteenth of August 2013
Pass
wednesteenth of January 2013
Pass
wednesteenth of February 2013
Pass
wednesteenth of June 2013
Pass
thursteenth of May 2013
Pass
thursteenth of June 2013
Pass
thursteenth of September 2013
Pass
friteenth of April 2013
Pass
friteenth of August 2013
Pass
friteenth of September 2013
Pass
saturteenth of February 2013
Pass
saturteenth of April 2013
Pass
saturteenth of October 2013
Pass
sunteenth of May 2013
Pass
sunteenth of June 2013
Pass
sunteenth of October 2013
Pass
first Monday of March 2013
Pass
first Monday of April 2013
Pass
first Tuesday of May 2013
Pass
first Tuesday of June 2013
Pass
first Wednesday of July 2013
Pass
first Wednesday of August 2013
Pass
first Thursday of September 2013
Pass
first Thursday of October 2013
Pass
first Friday of November 2013
Pass
first Friday of December 2013
Pass
first Saturday of January 2013
Pass
first Saturday of February 2013
Pass
first Sunday of March 2013
Pass
first Sunday of April 2013
Pass
second Monday of March 2013
Pass
second Monday of April 2013
Pass
second Tuesday of May 2013
Pass
second Tuesday of June 2013
Pass
second Wednesday of July 2013
Pass
second Wednesday of August 2013
Pass
second Thursday of September 2013
Pass
second Thursday of October 2013
Pass
second Friday of November 2013
Pass
second Friday of December 2013
Pass
second Saturday of January 2013
Pass
second Saturday of February 2013
Pass
second Sunday of March 2013
Pass
second Sunday of April 2013
Pass
third Monday of March 2013
Pass
third Monday of April 2013
Pass
third Tuesday of May 2013
Pass
third Tuesday of June 2013
Pass
third Wednesday of July 2013
Pass
third Wednesday of August 2013
Pass
third Thursday of September 2013
Pass
third Thursday of October 2013
Pass
third Friday of November 2013
Pass
third Friday of December 2013
Pass
third Saturday of January 2013
Pass
third Saturday of February 2013
Pass
third Sunday of March 2013
Pass
third Sunday of April 2013
Pass
fourth Monday of March 2013
Pass
fourth Monday of April 2013
Pass
fourth Tuesday of May 2013
Pass
fourth Tuesday of June 2013
Pass
fourth Wednesday of July 2013
Pass
fourth Wednesday of August 2013
Pass
fourth Thursday of September 2013
Pass
fourth Thursday of October 2013
Pass
fourth Friday of November 2013
Pass
fourth Friday of December 2013
Pass
fourth Saturday of January 2013
Pass
fourth Saturday of February 2013
Pass
fourth Sunday of March 2013
Pass
fourth Sunday of April 2013
Pass
last Monday of March 2013
Pass
last Monday of April 2013
Pass
last Tuesday of May 2013
Pass
last Tuesday of June 2013
Pass
last Wednesday of July 2013
Pass
last Wednesday of August 2013
Pass
last Thursday of September 2013
Pass
last Thursday of October 2013
Pass
last Friday of November 2013
Pass
last Friday of December 2013
Pass
last Saturday of January 2013
Pass
last Saturday of February 2013
Pass
last Sunday of March 2013
Pass
last Sunday of April 2013
Pass
last Wednesday of February 2012
Pass
last Wednesday of December 2014
Pass
last Sunday of February 2015
Pass
first Friday of December 2012
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.