kale

Finished
1111 * @returns {Date}
1212 */
1313export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
14 // Edge Case: Validate month input (1-12)
15 if (month < 1 || month > 12) {
16 throw new Error('Invalid month');
17 }
18
19 // Edge Case: Validate year input (positive integer)
20 if (year < 1) {
21 throw new Error('Invalid year');
22 }
23
24 // Map day names to numbers (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
25 const days = {
26 'sunday': 0,
27 'monday': 1,
28 'tuesday': 2,
29 'wednesday': 3,
30 'thursday': 4,
31 'friday': 5,
32 'saturday': 6
33 };
34
35 // Edge Case: Validate dayOfWeek input
36 const targetDay = days[dayOfWeek.toLowerCase()];
37 if (targetDay === undefined) {
38 throw new Error('Invalid day of week');
39 }
40
41 // Create a date object for the first day of the month
42 const firstDay = new Date(year, month - 1, 1);
43
44 // Edge Case: Handle first week
45 if (week === 'first') {
46 const dayOffset = (targetDay - firstDay.getDay() + 7) % 7;
47 return new Date(year, month - 1, 1 + dayOffset);
48 }
49
50 // Edge Case: Handle second week
51 if (week === 'second') {
52 const firstOccurrence = (targetDay - firstDay.getDay() + 7) % 7;
53 return new Date(year, month - 1, 1 + firstOccurrence + 7);
54 }
55
56 // Edge Case: Handle third week
57 if (week === 'third') {
58 const firstOccurrence = (targetDay - firstDay.getDay() + 7) % 7;
59 return new Date(year, month - 1, 1 + firstOccurrence + 14);
60 }
61
62 // Edge Case: Handle fourth week
63 if (week === 'fourth') {
64 const firstOccurrence = (targetDay - firstDay.getDay() + 7) % 7;
65 return new Date(year, month - 1, 1 + firstOccurrence + 21);
66 }
67
68 // Edge Case: Handle last week
69 if (week === 'last') {
70 // Get the last day of the month
71 const lastDay = new Date(year, month, 0);
72 // Find the last occurrence of the target day
73 const dayDiff = (lastDay.getDay() - targetDay + 7) % 7;
74 return new Date(lastDay.getFullYear(), lastDay.getMonth(), lastDay.getDate() - dayDiff);
75 }
76
77 // Edge Case: Handle teenth week
78 if (week === 'teenth') {
79 // Teenth days are from 13th to 19th
80 for (let day = 13; day <= 19; day++) {
81 const date = new Date(year, month - 1, day);
82 if (date.getDay() === targetDay) {
83 return date;
84 }
85 }
86 }
87
88 // Edge Case: Invalid week input
89 throw new Error('Invalid week descriptor');
90
91 // Handled Edge Cases: Invalid month, invalid year, invalid day of week, first week, second week, third week, fourth week, last week, teenth week, invalid week descriptor
1592};
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.