@ngel

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 given year, month, week, 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
1214 */
1315export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
16 // Validate inputs
17 if (typeof year !== 'number' || year < 1) {
18 throw new Error('Invalid year');
19 }
20
21 if (typeof month !== 'number' || month < 1 || month > 12) {
22 throw new Error('Invalid month');
23 }
24
25 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
26 if (!validWeeks.includes(week)) {
27 throw new Error('Invalid week');
28 }
29
30 const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
31 if (!validDays.includes(dayOfWeek)) {
32 throw new Error('Invalid day of week');
33 }
34
35 // Get the target day of week as a number (0-6)
36 const targetDay = validDays.indexOf(dayOfWeek);
37
38 // Create a date object for the first day of the month
39 const firstDay = new Date(year, month - 1, 1);
40
41 // Edge Case: Handle 'first' week
42 if (week === 'first') {
43 const firstDayOfWeek = firstDay.getDay();
44 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7;
45 return new Date(year, month - 1, 1 + daysToAdd);
46 }
47
48 // Edge Case: Handle 'second' week
49 if (week === 'second') {
50 const firstDayOfWeek = firstDay.getDay();
51 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7 + 7;
52 return new Date(year, month - 1, 1 + daysToAdd);
53 }
54
55 // Edge Case: Handle 'third' week
56 if (week === 'third') {
57 const firstDayOfWeek = firstDay.getDay();
58 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7 + 14;
59 return new Date(year, month - 1, 1 + daysToAdd);
60 }
61
62 // Edge Case: Handle 'fourth' week
63 if (week === 'fourth') {
64 const firstDayOfWeek = firstDay.getDay();
65 const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7 + 21;
66 return new Date(year, month - 1, 1 + daysToAdd);
67 }
68
69 // Edge Case: Handle 'last' week
70 if (week === 'last') {
71 // Get the last day of the month
72 const lastDay = new Date(year, month, 0);
73 const lastDayOfWeek = lastDay.getDay();
74 const daysToSubtract = (lastDayOfWeek - targetDay + 7) % 7;
75 return new Date(year, month - 1, lastDay.getDate() - daysToSubtract);
76 }
77
78 // Edge Case: Handle 'teenth' week
79 if (week === 'teenth') {
80 // Teenth days are 13-19
81 for (let day = 13; day <= 19; day++) {
82 const date = new Date(year, month - 1, day);
83 if (date.getDay() === targetDay) {
84 return date;
85 }
86 }
87 }
88
89 // This should never be reached due to validation, but included for completeness
90 throw new Error('Unable to calculate meetup date');
91
92 // Handled Edge Cases: Invalid year, invalid month, invalid week descriptor, invalid day of week,
93 // first week calculation, second week calculation, third week calculation, fourth week calculation,
94 // last week calculation, teenth week calculation
1595};
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.