hope

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 = {
2 sunday: 0,
3 monday: 1,
4 tuesday: 2,
5 wednesday: 3,
6 thursday: 4,
7 friday: 5,
8 saturday: 6
9};
10
11const weekDescriptors = {
12 first: 1,
13 second: 2,
14 third: 3,
15 fourth: 4
16};
517
618/**
719 * @param {number} year
1123 * @returns {Date}
1224 */
1325export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
26 // Edge Case: Invalid month (less than 1 or greater than 12)
27 if (month < 1 || month > 12) {
28 throw new Error('Invalid month');
29 }
30
31 // Edge Case: Invalid year (less than 1)
32 if (year < 1) {
33 throw new Error('Invalid year');
34 }
35
36 // Edge Case: Invalid day of week
37 if (!weekdays.hasOwnProperty(dayOfWeek.toLowerCase())) {
38 throw new Error('Invalid day of week');
39 }
40
41 const targetDay = weekdays[dayOfWeek.toLowerCase()];
42
43 // Edge Case: Invalid week descriptor
44 if (week !== 'first' && week !== 'second' && week !== 'third' &&
45 week !== 'fourth' && week !== 'last' && week !== 'teenth') {
46 throw new Error('Invalid week descriptor');
47 }
48
49 if (week === 'teenth') {
50 // Check days 13-19
51 for (let day = 13; day <= 19; day++) {
52 const date = new Date(year, month - 1, day);
53 if (date.getDay() === targetDay) {
54 return date;
55 }
56 }
57 } else if (week === 'last') {
58 // Get the last day of the month
59 const lastDay = new Date(year, month, 0).getDate();
60
61 // Iterate backwards from the end of the month
62 for (let day = lastDay; day >= 1; day--) {
63 const date = new Date(year, month - 1, day);
64 if (date.getDay() === targetDay) {
65 return date;
66 }
67 }
68 } else {
69 // Handle first, second, third, fourth
70 const occurrence = weekDescriptors[week];
71 let count = 0;
72
73 // Get the number of days in the month
74 const daysInMonth = new Date(year, month, 0).getDate();
75
76 for (let day = 1; day <= daysInMonth; day++) {
77 const date = new Date(year, month - 1, day);
78 if (date.getDay() === targetDay) {
79 count++;
80 if (count === occurrence) {
81 return date;
82 }
83 }
84 }
85
86 // Edge Case: Requested occurrence does not exist in the month
87 throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`);
88 }
89
90 // This should never be reached due to validation, but included for completeness
91 // Edge Case: No matching date found (should not happen with proper validation)
92 throw new Error('No matching date found');
1593};
94
95// Handled Edge Cases: Invalid month, Invalid year, Invalid day of week, Invalid week descriptor, Requested occurrence does not exist in the month, 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.