agent

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', 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 // Validate inputs
28 if (!days.hasOwnProperty(dayOfWeek)) {
29 throw new Error('Invalid day of week');
30 }
31
32 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
33 if (!validWeeks.includes(week)) {
34 throw new Error('Invalid week descriptor');
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 // Get the day of week for the first day (0 = Sunday, ..., 6 = Saturday)
41 const firstDayOfWeek = firstDay.getDay();
42
43 // Get the target day of week index
44 const targetDay = days[dayOfWeek];
45
46 // Calculate how many days to add to get to the first occurrence of target day
47 let daysToAdd = targetDay - firstDayOfWeek;
48 if (daysToAdd < 0) {
49 daysToAdd += 7;
50 }
51
52 // Date of the first occurrence of the target day in the month
53 const firstOccurrence = new Date(firstDay);
54 firstOccurrence.setDate(1 + daysToAdd);
55
56 // Handle each week descriptor
57 switch (week) {
58 case 'first':
59 return firstOccurrence;
60
61 case 'second':
62 const second = new Date(firstOccurrence);
63 second.setDate(firstOccurrence.getDate() + 7);
64 return second;
65
66 case 'third':
67 const third = new Date(firstOccurrence);
68 third.setDate(firstOccurrence.getDate() + 14);
69 return third;
70
71 case 'fourth':
72 const fourth = new Date(firstOccurrence);
73 fourth.setDate(firstOccurrence.getDate() + 21);
74 return fourth;
75
76 case 'last':
77 // Start with the first occurrence and keep adding 7 days
78 // until the next occurrence would be in the next month
79 let last = new Date(firstOccurrence);
80 let next = new Date(firstOccurrence);
81 next.setDate(last.getDate() + 7);
82
83 // Keep advancing while the next date is still in the same month
84 while (next.getMonth() === firstDay.getMonth()) {
85 last = next;
86 next = new Date(next);
87 next.setDate(next.getDate() + 7);
88 }
89 return last;
90
91 case 'teenth':
92 // Teenth days are from 13th to 19th
93 // Start with the first occurrence and add weeks until we're in the teenth range
94 let teenth = new Date(firstOccurrence);
95 while (teenth.getDate() < 13) {
96 teenth.setDate(teenth.getDate() + 7);
97 }
98 return teenth;
99
100 default:
101 throw new Error('Invalid week descriptor');
102 }
103
104 // Handled Edge Cases: Invalid day of week, invalid week descriptor, months with fewer than 4 occurrences of a day,
105 // last week of month with 5 occurrences of a day, teenth week calculation
106};
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.