seahawk

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, and day of week.
8 * @param {number} year - The year of the meetup
9 * @param {number} month - The month of the meetup (1-12)
10 * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth')
11 * @param {string} dayOfWeek - The day of the week ('Sunday', 'Monday', etc.)
12 * @returns {Date} - The calculated date of the meetup
1213 */
1314export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
15 // Map day names to their corresponding indices (0-6)
16 const days = {
17 sunday: 0,
18 monday: 1,
19 tuesday: 2,
20 wednesday: 3,
21 thursday: 4,
22 friday: 5,
23 saturday: 6
24 };
25
26 // Edge Case: Invalid day of week
27 if (!days.hasOwnProperty(dayOfWeek.toLowerCase())) {
28 throw new Error('Invalid day of week');
29 }
30
31 const targetDay = days[dayOfWeek.toLowerCase()];
32
33 // Edge Case: Invalid month
34 if (month < 1 || month > 12) {
35 throw new Error('Invalid month');
36 }
37
38 // Create a date object for the first day of the given month
39 const firstDay = new Date(year, month - 1, 1);
40
41 // Edge Case: Invalid year
42 if (isNaN(firstDay.getTime())) {
43 throw new Error('Invalid year');
44 }
45
46 if (week === 'teenth') {
47 // For teenth, we look for the day in the range 13-19
48 // Edge Case: Teenth week requested
49 for (let day = 13; day <= 19; day++) {
50 const date = new Date(year, month - 1, day);
51 if (date.getDay() === targetDay) {
52 return date;
53 }
54 }
55 } else if (week === 'last') {
56 // For last, we start from the last day of the month and work backwards
57 // Edge Case: Last week requested
58 const lastDay = new Date(year, month, 0); // 0th day of next month = last day of current month
59 const daysInMonth = lastDay.getDate();
60
61 for (let day = daysInMonth; day >= 1; day--) {
62 const date = new Date(year, month - 1, day);
63 if (date.getDay() === targetDay) {
64 return date;
65 }
66 }
67 } else {
68 // For first, second, third, fourth
69 // Edge Case: First/Second/Third/Fourth week requested
70 const weekMap = {
71 first: 1,
72 second: 2,
73 third: 3,
74 fourth: 4
75 };
76
77 // Edge Case: Invalid week descriptor
78 if (!weekMap.hasOwnProperty(week)) {
79 throw new Error('Invalid week descriptor');
80 }
81
82 const weekNumber = weekMap[week];
83 let occurrence = 0;
84
85 // Get the number of days in the month
86 const daysInMonth = new Date(year, month, 0).getDate();
87
88 for (let day = 1; day <= daysInMonth; day++) {
89 const date = new Date(year, month - 1, day);
90 if (date.getDay() === targetDay) {
91 occurrence++;
92 if (occurrence === weekNumber) {
93 return date;
94 }
95 }
96 }
97
98 // Edge Case: Requested week does not exist in the month
99 throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`);
100 }
101
102 // This should never be reached, but just in case
103 throw new Error('Could not determine the meetup date');
15104};
105
106// Handled Edge Cases: Invalid day of week, Invalid month, Invalid year, Teenth week requested, Last week requested, First/Second/Third/Fourth week requested, 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.