unicode

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 the week ('Sunday', 'Monday', etc.)
13 * @returns {Date} - The calculated date of the meetup
14 *
15 * @example
16 * meetup(2018, 1, 'first', 'Monday') // Returns Date for January 1, 2018
17 * meetup(1953, 8, 'teenth', 'Saturday') // Returns Date for August 15, 1953
1218 */
1319export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
20 // Validate inputs
21 // Edge Case: Invalid month values
22 if (month < 1 || month > 12) {
23 throw new Error('Invalid month');
24 }
25
26 // Edge Case: Invalid year values
27 if (year < 1) {
28 throw new Error('Invalid year');
29 }
30
31 // Edge Case: Invalid week descriptor
32 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
33 if (!validWeeks.includes(week)) {
34 throw new Error('Invalid week descriptor');
35 }
36
37 // Edge Case: Invalid day of week
38 const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
39 if (!validDays.includes(dayOfWeek)) {
40 throw new Error('Invalid day of week');
41 }
42
43 // Convert day of week to number (0-6)
44 const dayOfWeekNumber = validDays.indexOf(dayOfWeek);
45
46 // Create a date object for the first day of the month
47 const firstDay = new Date(year, month - 1, 1);
48
49 // Handle different week descriptors
50 switch (week) {
51 case 'first':
52 // Edge Case: First week - find the first occurrence of the day
53 return findNthOccurrence(firstDay, dayOfWeekNumber, 1);
54
55 case 'second':
56 // Edge Case: Second week - find the second occurrence of the day
57 return findNthOccurrence(firstDay, dayOfWeekNumber, 2);
58
59 case 'third':
60 // Edge Case: Third week - find the third occurrence of the day
61 return findNthOccurrence(firstDay, dayOfWeekNumber, 3);
62
63 case 'fourth':
64 // Edge Case: Fourth week - find the fourth occurrence of the day
65 return findNthOccurrence(firstDay, dayOfWeekNumber, 4);
66
67 case 'last':
68 // Edge Case: Last week - find the last occurrence of the day in the month
69 return findLastOccurrence(firstDay, dayOfWeekNumber);
70
71 case 'teenth':
72 // Edge Case: Teenth week - find the occurrence of the day in the teenth range (13-19)
73 return findTeenthOccurrence(firstDay, dayOfWeekNumber);
74
75 default:
76 throw new Error('Invalid week descriptor');
77 }
78
79 // Handled Edge Cases: Invalid month values, Invalid year values, Invalid week descriptor, Invalid day of week, First week, Second week, Third week, Fourth week, Last week, Teenth week
1580};
81
82/**
83 * Finds the nth occurrence of a specific day of the week in a month
84 *
85 * @param {Date} firstDay - The first day of the month
86 * @param {number} dayOfWeek - The target day of the week (0-6)
87 * @param {number} occurrence - Which occurrence to find (1st, 2nd, etc.)
88 * @returns {Date} - The date of the nth occurrence
89 */
90function findNthOccurrence(firstDay, dayOfWeek, occurrence) {
91 // Find the first occurrence of the day
92 let date = new Date(firstDay);
93
94 // If the first day is already the target day, we found the first occurrence
95 if (date.getDay() === dayOfWeek) {
96 // We already have the first occurrence
97 } else {
98 // Calculate days until the first occurrence of the target day
99 const daysUntil = (dayOfWeek - date.getDay() + 7) % 7;
100 date.setDate(date.getDate() + daysUntil);
101 }
102
103 // If we want the first occurrence, we're done
104 if (occurrence === 1) {
105 return date;
106 }
107
108 // Otherwise, add weeks to get to the nth occurrence
109 date.setDate(date.getDate() + (occurrence - 1) * 7);
110
111 // Edge Case: Check if the calculated date is still in the same month
112 if (date.getMonth() !== firstDay.getMonth()) {
113 throw new Error('That day does not exist in that week');
114 }
115
116 return date;
117 // Handled Edge Cases: Date not in same month
118}
119
120/**
121 * Finds the last occurrence of a specific day of the week in a month
122 *
123 * @param {Date} firstDay - The first day of the month
124 * @param {number} dayOfWeek - The target day of the week (0-6)
125 * @returns {Date} - The date of the last occurrence
126 */
127function findLastOccurrence(firstDay, dayOfWeek) {
128 // Get the first day of the next month
129 const nextMonth = new Date(firstDay.getFullYear(), firstDay.getMonth() + 1, 1);
130
131 // Go back one day to get the last day of the target month
132 const lastDay = new Date(nextMonth.getFullYear(), nextMonth.getMonth(), nextMonth.getDate() - 1);
133
134 // Find the last occurrence by working backwards
135 const daysSince = (lastDay.getDay() - dayOfWeek + 7) % 7;
136 const lastOccurrence = new Date(lastDay);
137 lastOccurrence.setDate(lastDay.getDate() - daysSince);
138
139 return lastOccurrence;
140 // Handled Edge Cases: None
141}
142
143/**
144 * Finds the occurrence of a specific day of the week in the teenth range (13-19)
145 *
146 * @param {Date} firstDay - The first day of the month
147 * @param {number} dayOfWeek - The target day of the week (0-6)
148 * @returns {Date} - The date of the teenth occurrence
149 */
150function findTeenthOccurrence(firstDay, dayOfWeek) {
151 // Start with the 13th of the month
152 const teenthStart = new Date(firstDay.getFullYear(), firstDay.getMonth(), 13);
153
154 // Find the first occurrence of the day on or after the 13th
155 const daysUntil = (dayOfWeek - teenthStart.getDay() + 7) % 7;
156 const teenthDate = new Date(teenthStart);
157 teenthDate.setDate(teenthStart.getDate() + daysUntil);
158
159 return teenthDate;
160 // Handled Edge Cases: None
161}
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.