\besttop

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 exact date of a meetup given 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 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(2020, 5, 'teenth', 'Wednesday') // Returns Date for May 13, 2020
1218 */
1319export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
20 // Validate inputs
21 if (typeof year !== 'number' || year < 1) {
22 throw new Error('Invalid year');
23 }
24
25 if (typeof month !== 'number' || month < 1 || month > 12) {
26 throw new Error('Invalid month');
27 }
28
29 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
30 if (!validWeeks.includes(week)) {
31 throw new Error('Invalid week');
32 }
33
34 const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
35 if (!validDays.includes(dayOfWeek)) {
36 throw new Error('Invalid day of week');
37 }
38
39 // Convert day name to index (0-6)
40 const dayIndex = validDays.indexOf(dayOfWeek);
41
42 // Create a date object for the first day of the month
43 const firstDay = new Date(year, month - 1, 1);
44
45 // Edge Case: Handle 'teenth' week (days 13-19)
46 if (week === 'teenth') {
47 // Start from the 13th day of the month
48 for (let day = 13; day <= 19; day++) {
49 const date = new Date(year, month - 1, day);
50 if (date.getDay() === dayIndex) {
51 return date;
52 }
53 }
54 // This should never happen with valid inputs
55 throw new Error('Could not find teenth day');
56 }
57
58 // Edge Case: Handle 'last' week
59 if (week === 'last') {
60 // Get the last day of the month
61 const lastDay = new Date(year, month, 0);
62 const daysInMonth = lastDay.getDate();
63
64 // Iterate backwards from the last day to find the last occurrence of the day
65 for (let day = daysInMonth; day >= 1; day--) {
66 const date = new Date(year, month - 1, day);
67 if (date.getDay() === dayIndex) {
68 return date;
69 }
70 }
71 // This should never happen with valid inputs
72 throw new Error('Could not find last day');
73 }
74
75 // Handle 'first', 'second', 'third', 'fourth' weeks
76 const weekIndex = validWeeks.indexOf(week);
77 let occurrenceCount = 0;
78
79 // Get the number of days in the month
80 const daysInMonth = new Date(year, month, 0).getDate();
81
82 // Iterate through each day of the month
83 for (let day = 1; day <= daysInMonth; day++) {
84 const date = new Date(year, month - 1, day);
85
86 // Check if this day matches the required day of week
87 if (date.getDay() === dayIndex) {
88 occurrenceCount++;
89
90 // If this is the required occurrence, return the date
91 if (occurrenceCount === weekIndex + 1) {
92 return date;
93 }
94 }
95 }
96
97 // Edge Case: Handle case where requested week doesn't exist in month
98 throw new Error(`The ${week} ${dayOfWeek} does not exist in ${month}/${year}`);
99
100 // Handled Edge Cases:
101 // 1. Invalid year (non-number or less than 1)
102 // 2. Invalid month (non-number or not in range 1-12)
103 // 3. Invalid week descriptor
104 // 4. Invalid day of week
105 // 5. Teenth week calculation
106 // 6. Last week calculation
107 // 7. Requested week doesn't exist in the month
15108};
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.