relax

Finished
44//
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 year, month, week, and day of week.
8 *
9 * @param {number} year - The year of the meetup
10 * @param {number} month - The month of the meetup (1-12)
11 * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth')
12 * @param {string} dayOfWeek - The day of the week ('Sunday', 'Monday', etc.)
13 * @returns {Date} - The calculated date of the meetup
1214 */
1315export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
15};
16 // Map day names to their index (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
17 const days = {
18 'Sunday': 0,
19 'Monday': 1,
20 'Tuesday': 2,
21 'Wednesday': 3,
22 'Thursday': 4,
23 'Friday': 5,
24 'Saturday': 6
25 };
26
27 // Edge Case: Invalid day of week
28 if (!(dayOfWeek in days)) {
29 throw new Error('Invalid day of week');
30 }
31
32 const dayIndex = days[dayOfWeek];
33
34 // Create a date object for the first day of the given month
35 const firstDay = new Date(year, month - 1, 1);
36
37 // Edge Case: Invalid month or year resulting in invalid date
38 if (firstDay.getMonth() !== month - 1) {
39 throw new Error('Invalid month or year');
40 }
41
42 // Find the first occurrence of the target day in the month
43 let firstOccurrence = firstDay.getDate() + (dayIndex - firstDay.getDay() + 7) % 7;
44
45 // If the first occurrence is in the next month, adjust to the previous week
46 if (firstOccurrence > 7) {
47 firstOccurrence -= 7;
48 }
49
50 // Create a date object for the first occurrence of the target day
51 const firstDate = new Date(year, month - 1, firstOccurrence);
52
53 // Handle different week descriptors
54 switch (week) {
55 case 'first':
56 return firstDate;
57
58 case 'second':
59 // Edge Case: Month has fewer than 2 occurrences of the day
60 const secondDate = new Date(year, month - 1, firstOccurrence + 7);
61 if (secondDate.getMonth() !== month - 1) {
62 throw new Error('Month has fewer than 2 occurrences of the specified day');
63 }
64 return secondDate;
65
66 case 'third':
67 // Edge Case: Month has fewer than 3 occurrences of the day
68 const thirdDate = new Date(year, month - 1, firstOccurrence + 14);
69 if (thirdDate.getMonth() !== month - 1) {
70 throw new Error('Month has fewer than 3 occurrences of the specified day');
71 }
72 return thirdDate;
73
74 case 'fourth':
75 // Edge Case: Month has fewer than 4 occurrences of the day
76 const fourthDate = new Date(year, month - 1, firstOccurrence + 21);
77 if (fourthDate.getMonth() !== month - 1) {
78 throw new Error('Month has fewer than 4 occurrences of the specified day');
79 }
80 return fourthDate;
81
82 case 'last':
83 // Find the last occurrence by starting from the first occurrence
84 // and adding weeks until we reach the next month
85 let lastDate = new Date(firstDate);
86 let nextDate = new Date(firstDate);
87 nextDate.setDate(nextDate.getDate() + 7);
88
89 while (nextDate.getMonth() === month - 1) {
90 lastDate = new Date(nextDate);
91 nextDate.setDate(nextDate.getDate() + 7);
92 }
93
94 return lastDate;
95
96 case 'teenth':
97 // Find the teenth occurrence (13th through 19th)
98 for (let day = 13; day <= 19; day++) {
99 const date = new Date(year, month - 1, day);
100 if (date.getDay() === dayIndex) {
101 return date;
102 }
103 }
104
105 // This should never happen with valid inputs
106 throw new Error('Could not find teenth occurrence');
107
108 default:
109 // Edge Case: Invalid week descriptor
110 throw new Error('Invalid week descriptor');
111 }
112
113 // Handled Edge Cases: Invalid day of week, Invalid month or year, Month has fewer than required occurrences of the day, Invalid week descriptor
114};
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.