Finished
33// convenience to get you started writing code faster.
44//
55
6export const meetup = () => {
7 throw new Error('Remove this line and implement the function');
6export const meetup = (year, month, week, dayOfWeek) => {
7 // Map day names to numbers (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
8 const days = {
9 'Sunday': 0,
10 'Monday': 1,
11 'Tuesday': 2,
12 'Wednesday': 3,
13 'Thursday': 4,
14 'Friday': 5,
15 'Saturday': 6
16 };
17
18 // Edge Case: Invalid month values
19 if (month < 1 || month > 12) {
20 throw new Error('Invalid month');
21 }
22
23 // Edge Case: Invalid year values
24 if (year < 1) {
25 throw new Error('Invalid year');
26 }
27
28 // Edge Case: Invalid day of week
29 if (!days.hasOwnProperty(dayOfWeek)) {
30 throw new Error('Invalid day of week');
31 }
32
33 const targetDay = days[dayOfWeek];
34 const firstDay = new Date(year, month - 1, 1);
35 const lastDay = new Date(year, month, 0);
36
37 // Edge Case: Invalid week values
38 if (!['first', 'second', 'third', 'fourth', 'last', 'teenth'].includes(week)) {
39 throw new Error('Invalid week');
40 }
41
42 if (week === 'first') {
43 // Find the first occurrence of the target day
44 const firstDayOfWeek = firstDay.getDay();
45 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7;
46 return new Date(year, month - 1, 1 + daysToAdd);
47 }
48
49 if (week === 'second') {
50 // Find the second occurrence of the target day
51 const firstDayOfWeek = firstDay.getDay();
52 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7;
53 return new Date(year, month - 1, 1 + daysToAdd + 7);
54 }
55
56 if (week === 'third') {
57 // Find the third occurrence of the target day
58 const firstDayOfWeek = firstDay.getDay();
59 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7;
60 return new Date(year, month - 1, 1 + daysToAdd + 14);
61 }
62
63 if (week === 'fourth') {
64 // Find the fourth occurrence of the target day
65 const firstDayOfWeek = firstDay.getDay();
66 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7;
67 return new Date(year, month - 1, 1 + daysToAdd + 21);
68 }
69
70 if (week === 'last') {
71 // Find the last occurrence of the target day
72 const lastDayOfWeek = lastDay.getDay();
73 const daysToSubtract = (lastDayOfWeek - targetDay + 7) % 7;
74 return new Date(year, month - 1, lastDay.getDate() - daysToSubtract);
75 }
76
77 if (week === 'teenth') {
78 // Find the teenth occurrence (13th through 19th) of the target day
79 for (let day = 13; day <= 19; day++) {
80 const date = new Date(year, month - 1, day);
81 if (date.getDay() === targetDay) {
82 return date;
83 }
84 }
85 }
86
87 // Edge Case: No matching date found (should not happen with valid inputs)
88 throw new Error('No matching date found');
889};
90
91// Handled Edge Cases: Invalid month values, Invalid year values, Invalid day of week, Invalid week values, No matching date found
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.