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 * Handled Edge Cases:
16 * - Invalid month values (outside 1-12)
17 * - Invalid week descriptors
18 * - Invalid day of week names
19 * - Dates that would fall outside the specified month
1220 */
1321export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
22 // Edge Case: Validate month is between 1 and 12
23 if (month < 1 || month > 12) {
24 throw new Error('Invalid month');
25 }
26
27 // Map day names to their corresponding index (0 = Sunday, 6 = Saturday)
28 const days = {
29 'Sunday': 0,
30 'Monday': 1,
31 'Tuesday': 2,
32 'Wednesday': 3,
33 'Thursday': 4,
34 'Friday': 5,
35 'Saturday': 6
36 };
37
38 // Edge Case: Validate dayOfWeek is a valid day name
39 if (!days.hasOwnProperty(dayOfWeek)) {
40 throw new Error('Invalid day of week');
41 }
42
43 const targetDayIndex = days[dayOfWeek];
44
45 // Create a date object for the first day of the given month
46 const firstDay = new Date(year, month - 1, 1);
47
48 // Edge Case: Validate week descriptor is one of the allowed values
49 switch(week) {
50 case 'first':
51 case 'second':
52 case 'third':
53 case 'fourth':
54 // For these cases, we find the first occurrence and add appropriate weeks
55 const weekOffset = {
56 'first': 0,
57 'second': 1,
58 'third': 2,
59 'fourth': 3
60 }[week];
61
62 // Find the date of the first occurrence of the target day
63 let firstOccurrence = firstDay.getDate() + (targetDayIndex - firstDay.getDay() + 7) % 7;
64 const targetDate = firstOccurrence + weekOffset * 7;
65
66 // Create the date and check if it's still in the same month
67 const resultDate = new Date(year, month - 1, targetDate);
68
69 // Edge Case: Check if the calculated date is still in the specified month
70 if (resultDate.getMonth() !== month - 1) {
71 throw new Error('No such occurrence in the given month');
72 }
73
74 return resultDate;
75
76 case 'last':
77 // For last, we find the last day of the month and work backwards
78 const lastDay = new Date(year, month, 0); // 0th day of next month = last day of current month
79 const daysInMonth = lastDay.getDate();
80
81 // Find the last occurrence of the target day
82 const lastDayOfWeek = lastDay.getDay();
83 const daysBack = (lastDayOfWeek - targetDayIndex + 7) % 7;
84 const lastOccurrenceDate = daysInMonth - daysBack;
85
86 return new Date(year, month - 1, lastOccurrenceDate);
87
88 case 'teenth':
89 // For teenth, we check days 13-19
90 for (let day = 13; day <= 19; day++) {
91 const testDate = new Date(year, month - 1, day);
92 if (testDate.getDay() === targetDayIndex) {
93 return testDate;
94 }
95 }
96
97 // This should never happen with valid inputs, but just in case
98 // Edge Case: No matching teenth day found (should not occur with valid inputs)
99 throw new Error('No teenth day found');
100
101 default:
102 // Edge Case: Invalid week descriptor
103 throw new Error('Invalid week descriptor');
104 }
15105};
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.