| 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(2020, 5, 'teenth', 'Wednesday') // Returns Date for May 13, 2020 |
| 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 = Sunday, 1 = Monday, ..., 6 = Saturday) |
| 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 | for (let day = 13; day <= 19; day++) { |
| 49 | const date = new Date(year, month - 1, day); |
| 50 | if (date.getDay() === dayOfWeekNumber) { |
| 51 | return date; |
| 52 | } |
| 53 | } |
| 54 | // This should never happen with valid inputs |
| 55 | throw new Error('Could not find teenth day'); |
| 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 | const daysInMonth = lastDay.getDate(); |
| 63 | |
| 64 | // Iterate backwards from the last day to find the last occurrence of the day |
| 65 | for (let day = daysInMonth; day >= 1; day--) { |
| 66 | const date = new Date(year, month - 1, day); |
| 67 | if (date.getDay() === dayOfWeekNumber) { |
| 68 | return date; |
| 69 | } |
| 70 | } |
| 71 | // This should never happen with valid inputs |
| 72 | throw new Error('Could not find last day'); |
| 73 | } |
| 74 | |
| 75 | // Handle 'first', 'second', 'third', 'fourth' weeks |
| 76 | const weekNumber = ['first', 'second', 'third', 'fourth'].indexOf(week) + 1; |
| 77 | |
| 78 | // Find the first occurrence of the day |
| 79 | let occurrenceCount = 0; |
| 80 | const daysInMonth = new Date(year, month, 0).getDate(); |
| 81 | |
| 82 | for (let day = 1; day <= daysInMonth; day++) { |
| 83 | const date = new Date(year, month - 1, day); |
| 84 | if (date.getDay() === dayOfWeekNumber) { |
| 85 | occurrenceCount++; |
| 86 | if (occurrenceCount === weekNumber) { |
| 87 | return date; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Edge Case: Requested week doesn't exist in the month (e.g., fifth Monday in a month with only 4 Mondays) |
| 93 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`); |
| 15 | 94 | }; |
| 95 | |
| 96 | // Handled Edge Cases: Invalid inputs (year, month, week, dayOfWeek), teenth week calculation, last week calculation, non-existent week in month |