weekends

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//
1/*
2 *
3 * This is only a SKELETON file for the 'Meetup' exercise. It's been provided as a
4 * convenience to get you started writing code faster.
5 *
6 */
57
68/**
7 * @param {number} year
8 * @param {number} month
9 * @param {string} week
10 * @param {string} dayOfWeek
11 * @returns {Date}
9 * Calculates the exact date of a meetup given the year, month, week, and day of the week.
10 *
11 * @param {number} year - The year of the meetup.
12 * @param {number} month - The month of the meetup (1-12).
13 * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth').
14 * @param {string} dayOfWeek - The day of the week ('Sunday', 'Monday', etc.).
15 * @returns {Date} - The calculated date of the meetup.
16 *
17 * Handled Edge Cases:
18 * - Invalid month values (outside 1-12)
19 * - Invalid week descriptors
20 * - Invalid day of week names
21 * - Non-existent dates (e.g., 30th of February)
22 * - Leap year calculations
1223 */
1324export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
25 // Edge Case: Validate month is between 1-12
26 if (month < 1 || month > 12) {
27 throw new Error('Invalid month');
28 }
29
30 // Edge Case: Validate year is a positive number
31 if (year < 1) {
32 throw new Error('Invalid year');
33 }
34
35 // Map day names to their corresponding index (0-6)
36 const days = {
37 'Sunday': 0,
38 'Monday': 1,
39 'Tuesday': 2,
40 'Wednesday': 3,
41 'Thursday': 4,
42 'Friday': 5,
43 'Saturday': 6
44 };
45
46 // Edge Case: Validate dayOfWeek is a valid day name
47 if (!days.hasOwnProperty(dayOfWeek)) {
48 throw new Error('Invalid day of week');
49 }
50
51 const targetDay = days[dayOfWeek];
52
53 // Create a date object for the first day of the month
54 const firstDay = new Date(year, month - 1, 1);
55
56 // Edge Case: Handle 'teenth' week (days 13-19)
57 if (week === 'teenth') {
58 // Start from the 13th day of the month
59 for (let day = 13; day <= 19; day++) {
60 const date = new Date(year, month - 1, day);
61 if (date.getDay() === targetDay) {
62 return date;
63 }
64 }
65 // This should never happen with valid inputs
66 throw new Error('Teenth day not found');
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 daysInMonth = lastDay.getDate();
74
75 // Iterate backwards from the last day to find the last occurrence of the target day
76 for (let day = daysInMonth; day >= 1; day--) {
77 const date = new Date(year, month - 1, day);
78 if (date.getDay() === targetDay) {
79 return date;
80 }
81 }
82 // This should never happen with valid inputs
83 throw new Error('Last day not found');
84 }
85
86 // Handle 'first', 'second', 'third', 'fourth' weeks
87 const weekMap = {
88 'first': 1,
89 'second': 2,
90 'third': 3,
91 'fourth': 4
92 };
93
94 // Edge Case: Validate week descriptor
95 if (!weekMap.hasOwnProperty(week)) {
96 throw new Error('Invalid week descriptor');
97 }
98
99 const occurrence = weekMap[week];
100 let count = 0;
101
102 // Get the number of days in the month
103 const daysInMonth = new Date(year, month, 0).getDate();
104
105 // Iterate through each day of the month
106 for (let day = 1; day <= daysInMonth; day++) {
107 const date = new Date(year, month - 1, day);
108 if (date.getDay() === targetDay) {
109 count++;
110 if (count === occurrence) {
111 return date;
112 }
113 }
114 }
115
116 // Edge Case: Requested occurrence doesn't exist in the month
117 throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`);
15118};
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.