Merry

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 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 the week ('Sunday', 'Monday', etc.)
13 * @returns {Date} - The calculated date of the meetup
14 *
15 * Handled Edge Cases:
16 * - Invalid month values (not 1-12)
17 * - Invalid week descriptors
18 * - Invalid day of week names
19 * - Dates that would result in invalid Date objects
1220 */
1321export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
22 // Validate inputs
23 if (month < 1 || month > 12) {
24 throw new Error('Invalid month');
25 }
26
27 const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
28 const dayIndex = daysOfWeek.indexOf(dayOfWeek);
29
30 // Edge Case: Invalid day of week
31 if (dayIndex === -1) {
32 throw new Error('Invalid day of week');
33 }
34
35 const weeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
36
37 // Edge Case: Invalid week descriptor
38 if (!weeks.includes(week)) {
39 throw new Error('Invalid week descriptor');
40 }
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: Invalid date (would happen with invalid year/month)
46 if (isNaN(firstDay.getTime())) {
47 throw new Error('Invalid date');
48 }
49
50 // Find the first occurrence of the target day in the month
51 let firstOccurrence = firstDay.getDate() + (dayIndex - firstDay.getDay() + 7) % 7;
52
53 switch (week) {
54 case 'first':
55 return new Date(year, month - 1, firstOccurrence);
56 case 'second':
57 return new Date(year, month - 1, firstOccurrence + 7);
58 case 'third':
59 return new Date(year, month - 1, firstOccurrence + 14);
60 case 'fourth':
61 return new Date(year, month - 1, firstOccurrence + 21);
62 case 'last':
63 // Start from the last day of the month and work backwards
64 const lastDay = new Date(year, month, 0);
65 const lastOccurrence = lastDay.getDate() - (lastDay.getDay() - dayIndex + 7) % 7;
66 return new Date(year, month - 1, lastOccurrence);
67 case 'teenth':
68 // Find the teenth occurrence (13th through 19th)
69 for (let day = 13; day <= 19; day++) {
70 const testDate = new Date(year, month - 1, day);
71 if (testDate.getDay() === dayIndex) {
72 return testDate;
73 }
74 }
75 // Edge Case: This should never happen with valid inputs, but just in case
76 throw new Error('Could not find teenth day');
77 default:
78 // Edge Case: This should never happen due to earlier validation
79 throw new Error('Invalid week descriptor');
80 }
81
82 // Handled Edge Cases: Invalid month values, Invalid day of week names, Invalid week descriptors, Invalid dates
1583};
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.