sn62

Finished
1//
2// This is only a SKELETON file for the 'Meetup' exercise. It's been provided as a
3// convenience to get you started writing code faster.
4//
1const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
52
63/**
7 * @param {number} year
8 * @param {number} month
9 * @param {string} week
10 * @param {string} dayOfWeek
11 * @returns {Date}
4 * Calculates the date of a meetup based on year, month, week descriptor, and day of week.
5 * @param {number} year - The year of the meetup
6 * @param {number} month - The month of the meetup (1-12)
7 * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth')
8 * @param {string} dayOfWeek - The day of week ('Sunday' through 'Saturday')
9 * @returns {Date} - The calculated date of the meetup
1210 */
1311export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
12 // Edge Case: Invalid month input (less than 1 or greater than 12)
13 if (month < 1 || month > 12) {
14 throw new Error('Invalid month');
15 }
16
17 // Edge Case: Invalid year input (less than 1)
18 if (year < 1) {
19 throw new Error('Invalid year');
20 }
21
22 // Edge Case: Invalid dayOfWeek input
23 if (!weekdays.includes(dayOfWeek)) {
24 throw new Error('Invalid day of week');
25 }
26
27 // Edge Case: Invalid week descriptor
28 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
29 if (!validWeeks.includes(week)) {
30 throw new Error('Invalid week descriptor');
31 }
32
33 const targetDayIndex = weekdays.indexOf(dayOfWeek);
34 const firstDayOfMonth = new Date(year, month - 1, 1);
35 const lastDayOfMonth = new Date(year, month, 0);
36
37 // Edge Case: Month with no matching day (should not occur with valid inputs but handled for robustness)
38 if (isNaN(firstDayOfMonth.getTime()) || isNaN(lastDayOfMonth.getTime())) {
39 throw new Error('Invalid date');
40 }
41
42 if (week === 'teenth') {
43 // Check days 13-19 for the target weekday
44 for (let day = 13; day <= 19; day++) {
45 const currentDate = new Date(year, month - 1, day);
46 if (currentDate.getDay() === targetDayIndex) {
47 return currentDate;
48 }
49 }
50 } else if (week === 'last') {
51 // Start from the last day of the month and work backwards
52 const daysInMonth = lastDayOfMonth.getDate();
53 for (let day = daysInMonth; day >= 1; day--) {
54 const currentDate = new Date(year, month - 1, day);
55 if (currentDate.getDay() === targetDayIndex) {
56 return currentDate;
57 }
58 }
59 } else {
60 // Handle first, second, third, fourth
61 const weekIndex = ['first', 'second', 'third', 'fourth'].indexOf(week);
62 let occurrenceCount = 0;
63
64 // Get the number of days in the month
65 const daysInMonth = lastDayOfMonth.getDate();
66
67 for (let day = 1; day <= daysInMonth; day++) {
68 const currentDate = new Date(year, month - 1, day);
69 if (currentDate.getDay() === targetDayIndex) {
70 occurrenceCount++;
71 if (occurrenceCount === weekIndex + 1) {
72 return currentDate;
73 }
74 }
75 }
76
77 // Edge Case: Requested week occurrence does not exist in the month (e.g., fifth Monday in a month with only 4 Mondays)
78 throw new Error(`That ${dayOfWeek} does not exist in the ${week} week of ${month}/${year}`);
79 }
80
81 // Edge Case: Teenth day not found (should not happen with valid inputs but included for completeness)
82 throw new Error(`That ${dayOfWeek} does not exist in the teenth week of ${month}/${year}`);
1583};
84
85// Handled Edge Cases: Invalid month, invalid year, invalid dayOfWeek, invalid week descriptor, month with no matching day, requested week occurrence does not exist, teenth day not 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.