goodagent

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//
1/*
2 * Meetup exercise implementation
3 * Finds the exact date of a meetup given year, month, week, and day of week
4 */
55
66/**
7 * @param {number} year
8 * @param {number} month
9 * @param {string} week
10 * @param {string} dayOfWeek
11 * @returns {Date}
7 * Calculates the date of a meetup based on the given criteria
8 * @param {number} year - The year of the meetup
9 * @param {number} month - The month of the meetup (1-12)
10 * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth')
11 * @param {string} dayOfWeek - The day of the week ('Sunday', 'Monday', etc.)
12 * @returns {Date} - The calculated date of the meetup
13 *
14 * Handled Edge Cases:
15 * - Invalid month values (outside 1-12)
16 * - Invalid week values (not one of the expected strings)
17 * - Invalid dayOfWeek values (not a valid day name)
18 * - No matching date found for the given criteria
1219 */
1320export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
21 // Edge Case: Invalid month values (outside 1-12)
22 if (month < 1 || month > 12) {
23 throw new Error('Invalid month');
24 }
25
26 // Edge Case: Invalid year values (negative or not a number)
27 if (year < 1) {
28 throw new Error('Invalid year');
29 }
30
31 // Map day names to their corresponding numbers (0=Sunday, 1=Monday, ..., 6=Saturday)
32 const days = {
33 'Sunday': 0,
34 'Monday': 1,
35 'Tuesday': 2,
36 'Wednesday': 3,
37 'Thursday': 4,
38 'Friday': 5,
39 'Saturday': 6
40 };
41
42 // Edge Case: Invalid dayOfWeek values (not a valid day name)
43 if (!days.hasOwnProperty(dayOfWeek)) {
44 throw new Error('Invalid day of week');
45 }
46
47 const targetDay = days[dayOfWeek];
48
49 // Edge Case: Invalid week values (not one of the expected strings)
50 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
51 if (!validWeeks.includes(week)) {
52 throw new Error('Invalid week');
53 }
54
55 // Create a date object for the first day of the month
56 const firstDay = new Date(year, month - 1, 1);
57
58 // For 'teenth' week, check days 13-19
59 if (week === 'teenth') {
60 for (let day = 13; day <= 19; day++) {
61 const date = new Date(year, month - 1, day);
62 if (date.getDay() === targetDay) {
63 return date;
64 }
65 }
66 }
67
68 // For 'last' week, start from the last day of the month and work backwards
69 else if (week === 'last') {
70 // Get the last day of the month
71 const lastDay = new Date(year, month, 0);
72 const daysInMonth = lastDay.getDate();
73
74 for (let day = daysInMonth; day >= 1; day--) {
75 const date = new Date(year, month - 1, day);
76 if (date.getDay() === targetDay) {
77 return date;
78 }
79 }
80 }
81
82 // For 'first', 'second', 'third', 'fourth', count occurrences
83 else {
84 const weekNumbers = {
85 'first': 1,
86 'second': 2,
87 'third': 3,
88 'fourth': 4
89 };
90
91 let count = 0;
92 const daysInMonth = new Date(year, month, 0).getDate();
93
94 for (let day = 1; day <= daysInMonth; day++) {
95 const date = new Date(year, month - 1, day);
96 if (date.getDay() === targetDay) {
97 count++;
98 if (count === weekNumbers[week]) {
99 return date;
100 }
101 }
102 }
103
104 // Edge Case: No matching date found for the given criteria
105 throw new Error('No matching date found');
106 }
107
108 // Edge Case: No matching date found for the given criteria (for teenth and last cases)
109 throw new Error('No matching date found');
15110};
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.