| 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 exact 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 |
| 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 (typeof year !== 'number' || year < 1) { |
| 22 | throw new Error('Invalid year'); |
| 23 | } |
| 24 | |
| 25 | if (typeof month !== 'number' || 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'); |
| 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 day name to index (0-6) |
| 40 | const dayIndex = 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 | const daysUntilTarget = (dayIndex - teenthStart.getDay() + 7) % 7; |
| 52 | const resultDate = new Date(teenthStart); |
| 53 | resultDate.setDate(13 + daysUntilTarget); |
| 54 | |
| 55 | return resultDate; |
| 56 | } |
| 57 | |
| 58 | // Edge Case: Handle 'last' week |
| 59 | if (week === 'last') { |
| 60 | // Get the last day of the month |
| 61 | const lastDay = new Date(year, month, 0); |
| 62 | |
| 63 | // Find the last occurrence of the target day |
| 64 | const daysFromTarget = (lastDay.getDay() - dayIndex + 7) % 7; |
| 65 | const resultDate = new Date(lastDay); |
| 66 | resultDate.setDate(lastDay.getDate() - daysFromTarget); |
| 67 | |
| 68 | return resultDate; |
| 69 | } |
| 70 | |
| 71 | // Edge Case: Handle 'first', 'second', 'third', 'fourth' weeks |
| 72 | // Find the first occurrence of the target day in the month |
| 73 | const firstOccurrence = (dayIndex - firstDay.getDay() + 7) % 7 + 1; |
| 74 | |
| 75 | // Calculate the target date based on the week |
| 76 | const weekIndex = ['first', 'second', 'third', 'fourth'].indexOf(week); |
| 77 | const targetDate = firstOccurrence + (weekIndex * 7); |
| 78 | |
| 79 | // Edge Case: Validate that the calculated date is within the month |
| 80 | const resultDate = new Date(year, month - 1, targetDate); |
| 81 | |
| 82 | // If the month doesn't match, the date is invalid (e.g., 5th Monday in a month with only 4 Mondays) |
| 83 | if (resultDate.getMonth() !== month - 1) { |
| 84 | throw new Error('Invalid date'); |
| 85 | } |
| 86 | |
| 87 | return resultDate; |
| 88 | |
| 89 | // Handled Edge Cases: |
| 90 | // 1. Invalid year input (non-number or less than 1) |
| 91 | // 2. Invalid month input (non-number or not in range 1-12) |
| 92 | // 3. Invalid week input (not one of the valid week descriptors) |
| 93 | // 4. Invalid day of week input (not one of the valid day names) |
| 94 | // 5. Teenth week calculation (finding day in 13-19 range) |
| 95 | // 6. Last week calculation (finding last occurrence of day in month) |
| 96 | // 7. First/second/third/fourth week calculations |
| 97 | // 8. Date validation (ensuring calculated date exists in the specified month) |
| 15 | 98 | }; |