corpus

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 descriptor, 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 week ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
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');
16 // Validate inputs
17 if (typeof year !== 'number' || year < 1) {
18 throw new Error('Invalid year');
19 }
20
21 if (typeof month !== 'number' || month < 1 || month > 12) {
22 throw new Error('Invalid month');
23 }
24
25 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
26 if (!validWeeks.includes(week)) {
27 throw new Error('Invalid week descriptor');
28 }
29
30 const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
31 if (!validDays.includes(dayOfWeek)) {
32 throw new Error('Invalid day of week');
33 }
34
35 // Convert dayOfWeek to number (0-6)
36 const targetDay = validDays.indexOf(dayOfWeek);
37
38 // Create a date object for the first day of the month
39 const firstDay = new Date(year, month - 1, 1);
40
41 // Edge Case: Handle 'teenth' week
42 if (week === 'teenth') {
43 // Start from the 13th day of the month
44 const teenthDate = new Date(year, month - 1, 13);
45
46 // Find the first occurrence of the target day on or after the 13th
47 const dayDiff = (targetDay - teenthDate.getDay() + 7) % 7;
48 const resultDate = new Date(teenthDate);
49 resultDate.setDate(teenthDate.getDate() + dayDiff);
50
51 // Verify the result is still in the same month (should always be true for valid inputs)
52 if (resultDate.getMonth() !== month - 1) {
53 throw new Error('Calculation error: teenth date is not in the correct month');
54 }
55
56 return resultDate;
57 }
58
59 // Edge Case: Handle 'last' week
60 if (week === 'last') {
61 // Get the last day of the month
62 const lastDay = new Date(year, month, 0);
63
64 // Find the last occurrence of the target day
65 const dayDiff = (lastDay.getDay() - targetDay + 7) % 7;
66 const resultDate = new Date(lastDay);
67 resultDate.setDate(lastDay.getDate() - dayDiff);
68
69 return resultDate;
70 }
71
72 // Handle 'first', 'second', 'third', 'fourth'
73 const weekNumber = ['first', 'second', 'third', 'fourth'].indexOf(week) + 1;
74
75 // Find the first occurrence of the target day in the month
76 const firstTargetDay = (targetDay - firstDay.getDay() + 7) % 7 + 1;
77
78 // Calculate the date of the target occurrence
79 const targetDate = firstTargetDay + (weekNumber - 1) * 7;
80
81 // Edge Case: Handle when calculated date exceeds the number of days in the month
82 const resultDate = new Date(year, month - 1, targetDate);
83
84 // Verify that the result is in the correct month
85 if (resultDate.getMonth() !== month - 1) {
86 throw new Error(`The ${week} ${dayOfWeek} does not exist in ${year}-${month}`);
87 }
88
89 return resultDate;
90
91 // Handled Edge Cases:
92 // 1. Invalid year (non-number or less than 1)
93 // 2. Invalid month (non-number or not in range 1-12)
94 // 3. Invalid week descriptor (not one of the valid strings)
95 // 4. Invalid day of week (not one of the valid day names)
96 // 5. Teenth week calculation
97 // 6. Last week calculation
98 // 7. When calculated date exceeds the number of days in the month
1599};
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.