kale

Finished
1111 * @returns {Date}
1212 */
1313export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
14 // Edge Case: Validate inputs
15 if (typeof year !== 'number' || typeof month !== 'number' || typeof week !== 'string' || typeof dayOfWeek !== 'string') {
16 throw new Error('Invalid input types');
17 }
18
19 // Edge Case: Validate month range
20 if (month < 1 || month > 12) {
21 throw new Error('Invalid month');
22 }
23
24 // Edge Case: Validate year range
25 if (year < 1) {
26 throw new Error('Invalid year');
27 }
28
29 // Map day names to numbers (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
30 const days = {
31 'sunday': 0,
32 'monday': 1,
33 'tuesday': 2,
34 'wednesday': 3,
35 'thursday': 4,
36 'friday': 5,
37 'saturday': 6
38 };
39
40 // Edge Case: Validate dayOfWeek
41 const targetDay = days[dayOfWeek.toLowerCase()];
42 if (targetDay === undefined) {
43 throw new Error('Invalid day of week');
44 }
45
46 // Edge Case: Validate week descriptor
47 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
48 if (!validWeeks.includes(week.toLowerCase())) {
49 throw new Error('Invalid week descriptor');
50 }
51
52 // Create a date object for the first day of the month
53 const firstDay = new Date(year, month - 1, 1);
54
55 // Edge Case: Month with fewer than 31 days
56 // This is handled by JavaScript Date automatically when we create dates
57
58 // For 'teenth' week
59 if (week.toLowerCase() === 'teenth') {
60 // Start checking from the 13th day of the month
61 for (let day = 13; day <= 19; day++) {
62 const date = new Date(year, month - 1, day);
63 if (date.getDay() === targetDay) {
64 return date;
65 }
66 }
67 }
68
69 // For 'last' week
70 else if (week.toLowerCase() === 'last') {
71 // Get the last day of the month
72 const lastDay = new Date(year, month, 0);
73 const daysInMonth = lastDay.getDate();
74
75 // Work backwards from the last day to find the last occurrence of the target day
76 for (let day = daysInMonth; day >= 1; day--) {
77 const date = new Date(year, month - 1, day);
78 if (date.getDay() === targetDay) {
79 return date;
80 }
81 }
82 }
83
84 // For 'first', 'second', 'third', 'fourth'
85 else {
86 const weekNumber = ['first', 'second', 'third', 'fourth'].indexOf(week.toLowerCase()) + 1;
87
88 // Find the first occurrence of the target day in the month
89 const firstDayOfWeek = firstDay.getDay();
90 let daysUntilTarget = targetDay - firstDayOfWeek;
91
92 // Edge Case: Month starting on the requested weekday
93 if (daysUntilTarget < 0) {
94 daysUntilTarget += 7;
95 }
96
97 const firstOccurrence = 1 + daysUntilTarget;
98 const targetDate = firstOccurrence + (weekNumber - 1) * 7;
99
100 // Edge Case: Requested nth weekday not existing
101 const lastDayOfMonth = new Date(year, month, 0).getDate();
102 if (targetDate > lastDayOfMonth) {
103 throw new Error('Requested weekday does not exist in the month');
104 }
105
106 return new Date(year, month - 1, targetDate);
107 }
108
109 // This should never be reached due to input validation
110 throw new Error('Unable to determine meetup date');
15111};
112
113// Handled Edge Cases: Invalid input types, invalid month range, invalid year, invalid day of week, invalid week descriptor, month with fewer than 31 days, month starting on the requested weekday, requested nth weekday not existing
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.