| 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 (!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'); |
| 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 | // Map day names to numbers (0-6) |
| 40 | const dayOfWeekNumber = 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() !== dayOfWeekNumber) { |
| 53 | currentDay.setDate(currentDay.getDate() + 1); |
| 54 | } |
| 55 | |
| 56 | // Verify we're still in the same month (should always be true for valid inputs) |
| 57 | if (currentDay.getMonth() !== month - 1) { |
| 58 | throw new Error('Could not find teenth day'); |
| 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); |
| 68 | |
| 69 | // Find the last occurrence of the target day |
| 70 | let currentDay = new Date(lastDay); |
| 71 | while (currentDay.getDay() !== dayOfWeekNumber) { |
| 72 | currentDay.setDate(currentDay.getDate() - 1); |
| 73 | } |
| 74 | |
| 75 | return currentDay; |
| 76 | } |
| 77 | |
| 78 | // Handle 'first', 'second', 'third', 'fourth' weeks |
| 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 currentDay = new Date(firstDay); |
| 83 | while (currentDay.getDay() !== dayOfWeekNumber) { |
| 84 | currentDay.setDate(currentDay.getDate() + 1); |
| 85 | } |
| 86 | |
| 87 | // Add the appropriate number of weeks |
| 88 | currentDay.setDate(currentDay.getDate() + (weekNumber - 1) * 7); |
| 89 | |
| 90 | // Edge Case: Verify the calculated date is still in the correct month |
| 91 | if (currentDay.getMonth() !== month - 1) { |
| 92 | throw new Error(`There is no ${week} ${dayOfWeek} in the specified month`); |
| 93 | } |
| 94 | |
| 95 | return currentDay; |
| 96 | |
| 97 | // Handled Edge Cases: |
| 98 | // 1. Invalid year (non-integer or less than 1) |
| 99 | // 2. Invalid month (non-integer or not in range 1-12) |
| 100 | // 3. Invalid week descriptor |
| 101 | // 4. Invalid day of week |
| 102 | // 5. Teenth week calculation |
| 103 | // 6. Last week calculation |
| 104 | // 7. Verification that calculated date is in the correct month |
| 15 | 105 | }; |