| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 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 descriptor, 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 |
| 14 | * |
| 15 | * @example |
| 16 | * meetup(2018, 1, 'first', 'Monday') // Returns Date for January 1, 2018 |
| 17 | * meetup(1953, 8, 'teenth', 'Saturday') // Returns Date for August 15, 1953 |
| 12 | 18 | */ |
| 13 | 19 | export const meetup = (year, month, week, dayOfWeek) => { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 20 | // Validate inputs |
| 21 | if (!Number.isInteger(year) || year < 1) { |
| 22 | throw new Error('Invalid year'); |
| 23 | } |
| 24 | |
| 25 | if (!Number.isInteger(month) || month < 1 || month > 12) { |
| 26 | throw new Error('Invalid month'); |
| 27 | } |
| 28 | |
| 29 | const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth']; |
| 30 | if (!validWeeks.includes(week)) { |
| 31 | throw new Error('Invalid week descriptor'); |
| 32 | } |
| 33 | |
| 34 | const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
| 35 | if (!validDays.includes(dayOfWeek)) { |
| 36 | throw new Error('Invalid day of week'); |
| 37 | } |
| 38 | |
| 39 | // Convert dayOfWeek to number (0-6, Sunday-Saturday) |
| 40 | const targetDay = validDays.indexOf(dayOfWeek); |
| 41 | |
| 42 | // Create a date object for the first day of the month |
| 43 | const firstDay = new Date(year, month - 1, 1); |
| 44 | |
| 45 | // Edge Case: Handle 'teenth' week (days 13-19) |
| 46 | if (week === 'teenth') { |
| 47 | // Start from the 13th day of the month |
| 48 | const teenthStart = new Date(year, month - 1, 13); |
| 49 | |
| 50 | // Find the first occurrence of the target day on or after the 13th |
| 51 | let currentDay = teenthStart; |
| 52 | while (currentDay.getDay() !== targetDay) { |
| 53 | currentDay.setDate(currentDay.getDate() + 1); |
| 54 | } |
| 55 | |
| 56 | // Verify we're still in the teenth range (should always be true by logic) |
| 57 | if (currentDay.getDate() < 13 || currentDay.getDate() > 19) { |
| 58 | throw new Error('Logic error: teenth day out of range'); |
| 59 | } |
| 60 | |
| 61 | return currentDay; |
| 62 | } |
| 63 | |
| 64 | // Edge Case: Handle 'last' week |
| 65 | if (week === 'last') { |
| 66 | // Get the last day of the month |
| 67 | const lastDay = new Date(year, month, 0); // 0th day of next month = last day of current month |
| 68 | |
| 69 | // Find the last occurrence of the target day |
| 70 | let currentDay = new Date(lastDay); |
| 71 | while (currentDay.getDay() !== targetDay) { |
| 72 | currentDay.setDate(currentDay.getDate() - 1); |
| 73 | } |
| 74 | |
| 75 | return currentDay; |
| 76 | } |
| 77 | |
| 78 | // Edge Case: Handle ordinal weeks (first, second, third, fourth) |
| 79 | const weekNumber = ['first', 'second', 'third', 'fourth'].indexOf(week) + 1; |
| 80 | |
| 81 | // Find the first occurrence of the target day in the month |
| 82 | let firstOccurrence = new Date(firstDay); |
| 83 | while (firstOccurrence.getDay() !== targetDay) { |
| 84 | firstOccurrence.setDate(firstOccurrence.getDate() + 1); |
| 85 | } |
| 86 | |
| 87 | // Calculate the target occurrence |
| 88 | const targetDate = new Date(firstOccurrence); |
| 89 | targetDate.setDate(firstOccurrence.getDate() + (weekNumber - 1) * 7); |
| 90 | |
| 91 | // Edge Case: Verify the calculated date is still in the correct month |
| 92 | if (targetDate.getMonth() !== firstDay.getMonth()) { |
| 93 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`); |
| 94 | } |
| 95 | |
| 96 | return targetDate; |
| 15 | 97 | }; |
| 98 | |
| 99 | // Handled Edge Cases: Invalid inputs (year, month, week, dayOfWeek), teenth week calculation, last week calculation, ordinal weeks calculation, date overflow to next month |