jackson

Finished
1111 * @returns {Date}
1212 */
1313export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
14 // Create a date object for the first day of the month
15 const firstDay = new Date(year, month - 1, 1);
16
17 // Get the last day of the month
18 const lastDay = new Date(year, month, 0);
19
20 // Map day names to numbers (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
21 const days = {
22 'sunday': 0,
23 'monday': 1,
24 'tuesday': 2,
25 'wednesday': 3,
26 'thursday': 4,
27 'friday': 5,
28 'saturday': 6
29 };
30
31 const targetDay = days[dayOfWeek.toLowerCase()];
32
33 // Edge Case: Invalid day of week
34 if (targetDay === undefined) {
35 throw new Error('Invalid day of week');
36 }
37
38 // Edge Case: Invalid week descriptor
39 if (!['first', 'second', 'third', 'fourth', 'last', 'teenth'].includes(week.toLowerCase())) {
40 throw new Error('Invalid week descriptor');
41 }
42
43 if (week.toLowerCase() === 'teenth') {
44 // Check days 13-19
45 for (let day = 13; day <= 19; day++) {
46 const date = new Date(year, month - 1, day);
47 if (date.getDay() === targetDay) {
48 return date;
49 }
50 }
51 } else if (week.toLowerCase() === 'last') {
52 // Start from the last day of the month and work backwards
53 for (let d = lastDay.getDate(); d >= 1; d--) {
54 const date = new Date(year, month - 1, d);
55 if (date.getDay() === targetDay) {
56 return date;
57 }
58 }
59 } else {
60 // For first, second, third, fourth
61 const ordinals = {
62 'first': 1,
63 'second': 2,
64 'third': 3,
65 'fourth': 4
66 };
67
68 const ordinal = ordinals[week.toLowerCase()];
69 let count = 0;
70
71 // Edge Case: Invalid ordinal week
72 if (ordinal === undefined) {
73 throw new Error('Invalid week descriptor');
74 }
75
76 // Iterate through all days of the month
77 for (let d = 1; d <= lastDay.getDate(); d++) {
78 const date = new Date(year, month - 1, d);
79 if (date.getDay() === targetDay) {
80 count++;
81 if (count === ordinal) {
82 return date;
83 }
84 }
85 }
86
87 // Edge Case: Requested week does not exist in the month
88 throw new Error('Requested week does not exist in the month');
89 }
90
91 // This should never be reached due to the constraints, but just in case
92 throw new Error('No matching date found');
1593};
94
95// Handled Edge Cases: Invalid day of week, Invalid week descriptor, Requested week does not exist in the month
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.