let it go

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 * @param {number} year - The year of the meetup
9 * @param {number} month - The month of the meetup (1-12)
10 * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth')
11 * @param {string} dayOfWeek - The day of the week ('Sunday', 'Monday', etc.)
12 * @returns {Date} - The calculated date of the meetup
1213 */
1314export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
15 // Validate inputs
16 // Edge Case: Invalid month values
17 if (month < 1 || month > 12) {
18 throw new Error('Invalid month');
19 }
20
21 // Edge Case: Invalid year values
22 if (year < 1) {
23 throw new Error('Invalid year');
24 }
25
26 // Map day names to numbers (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
27 const days = {
28 'sunday': 0,
29 'monday': 1,
30 'tuesday': 2,
31 'wednesday': 3,
32 'thursday': 4,
33 'friday': 5,
34 'saturday': 6
35 };
36
37 // Edge Case: Invalid day of week
38 const targetDay = days[dayOfWeek.toLowerCase()];
39 if (targetDay === undefined) {
40 throw new Error('Invalid day of week');
41 }
42
43 // Create a date object for the first day of the month
44 const firstDay = new Date(year, month - 1, 1);
45
46 // Edge Case: Invalid week descriptor
47 switch(week.toLowerCase()) {
48 case 'first':
49 // Find the first occurrence of the target day
50 const firstDiff = (targetDay - firstDay.getDay() + 7) % 7;
51 return new Date(year, month - 1, 1 + firstDiff);
52
53 case 'second':
54 // Find the first occurrence and add 7 days
55 const secondDiff = (targetDay - firstDay.getDay() + 7) % 7;
56 return new Date(year, month - 1, 1 + secondDiff + 7);
57
58 case 'third':
59 // Find the first occurrence and add 14 days
60 const thirdDiff = (targetDay - firstDay.getDay() + 7) % 7;
61 return new Date(year, month - 1, 1 + thirdDiff + 14);
62
63 case 'fourth':
64 // Find the first occurrence and add 21 days
65 const fourthDiff = (targetDay - firstDay.getDay() + 7) % 7;
66 return new Date(year, month - 1, 1 + fourthDiff + 21);
67
68 case 'last':
69 // Find the last day of the month
70 const lastDay = new Date(year, month, 0);
71 // Find the last occurrence of the target day
72 const lastDiff = (lastDay.getDay() - targetDay + 7) % 7;
73 return new Date(year, month - 1, lastDay.getDate() - lastDiff);
74
75 case 'teenth':
76 // Find the first day of the teenth range (13th)
77 const teenthBase = new Date(year, month - 1, 13);
78 // Calculate how many days to add to reach the target day
79 const teenthDiff = (targetDay - teenthBase.getDay() + 7) % 7;
80 return new Date(year, month - 1, 13 + teenthDiff);
81
82 default:
83 throw new Error('Invalid week descriptor');
84 }
85
86 // Handled Edge Cases: Invalid month values, Invalid year values, Invalid day of week, Invalid week descriptor
1587};
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.