Finished
1111 * @returns {Date}
1212 */
1313export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
14 // Map day names to their index (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
15 const daysOfWeek = {
16 'Sunday': 0,
17 'Monday': 1,
18 'Tuesday': 2,
19 'Wednesday': 3,
20 'Thursday': 4,
21 'Friday': 5,
22 'Saturday': 6
23 };
24
25 // Edge Case: Invalid day of week
26 if (!(dayOfWeek in daysOfWeek)) {
27 throw new Error('Invalid day of week');
28 }
29
30 const targetDayIndex = daysOfWeek[dayOfWeek];
31
32 // Edge Case: Invalid month (should be 1-12)
33 if (month < 1 || month > 12) {
34 throw new Error('Invalid month');
35 }
36
37 // Create a date object for the first day of the given month
38 const firstDay = new Date(year, month - 1, 1);
39
40 // Edge Case: Invalid year (Date constructor handles this, but we can validate if needed)
41
42 // Handle different week descriptors
43 if (week === 'teenth') {
44 // For teenth, we check days 13 through 19
45 for (let day = 13; day <= 19; day++) {
46 const currentDate = new Date(year, month - 1, day);
47 if (currentDate.getDay() === targetDayIndex) {
48 return currentDate;
49 }
50 }
51 // Edge Case: No matching day found in teenth range (should not happen with valid inputs)
52 throw new Error('No matching day found in teenth range');
53 } else if (week === 'last') {
54 // For last, we start from the last day of the month and work backwards
55 const lastDay = new Date(year, month, 0); // 0 gives us the last day of the previous month
56 const daysInMonth = lastDay.getDate();
57
58 for (let day = daysInMonth; day >= 1; day--) {
59 const currentDate = new Date(year, month - 1, day);
60 if (currentDate.getDay() === targetDayIndex) {
61 return currentDate;
62 }
63 }
64 // Edge Case: No matching day found in month (should not happen with valid inputs)
65 throw new Error('No matching day found in month');
66 } else {
67 // For first, second, third, fourth
68 const weekNumbers = {
69 'first': 1,
70 'second': 2,
71 'third': 3,
72 'fourth': 4
73 };
74
75 // Edge Case: Invalid week descriptor
76 if (!(week in weekNumbers)) {
77 throw new Error('Invalid week descriptor');
78 }
79
80 const weekNumber = weekNumbers[week];
81 let occurrenceCount = 0;
82
83 // Get the number of days in the month
84 const daysInMonth = new Date(year, month, 0).getDate();
85
86 for (let day = 1; day <= daysInMonth; day++) {
87 const currentDate = new Date(year, month - 1, day);
88 if (currentDate.getDay() === targetDayIndex) {
89 occurrenceCount++;
90 if (occurrenceCount === weekNumber) {
91 return currentDate;
92 }
93 }
94 }
95
96 // Edge Case: Requested week number exceeds available occurrences
97 throw new Error(`Month ${month} of year ${year} does not have a ${week} ${dayOfWeek}`);
98 }
99
100 // Edge Case: Unexpected code path
101 throw new Error('Unexpected error in meetup calculation');
15102};
103
104// Handled Edge Cases: Invalid day of week, Invalid month, No matching day found in teenth range, No matching day found in month, Invalid week descriptor, Requested week number exceeds available occurrences, Unexpected code path
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.