| 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 | // 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 matching day of week on or after the 13th |
| 51 | const teenthDay = new Date(teenthStart); |
| 52 | while (teenthDay.getDay() !== dayIndex) { |
| 53 | teenthDay.setDate(teenthDay.getDate() + 1); |
| 54 | } |
| 55 | |
| 56 | // Verify it's still in the teenth range (should always be) |
| 57 | if (teenthDay.getDate() > 19) { |
| 58 | throw new Error('No teenth day found'); |
| 59 | } |
| 60 | |
| 61 | return teenthDay; |
| 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 matching day of week in the month |
| 70 | const lastWeekDay = new Date(lastDay); |
| 71 | while (lastWeekDay.getDay() !== dayIndex) { |
| 72 | lastWeekDay.setDate(lastWeekDay.getDate() - 1); |
| 73 | } |
| 74 | |
| 75 | return lastWeekDay; |
| 76 | } |
| 77 | |
| 78 | // Edge Case: Handle ordinal weeks (first, second, third, fourth) |
| 79 | const weekIndex = validWeeks.indexOf(week); |
| 80 | |
| 81 | // Find the first matching day of week in the month |
| 82 | const firstMatchingDay = new Date(firstDay); |
| 83 | |
| 84 | // If the first day is after the target day, move to next week |
| 85 | if (firstDay.getDay() > dayIndex) { |
| 86 | firstMatchingDay.setDate(firstMatchingDay.getDate() + (7 - firstDay.getDay() + dayIndex)); |
| 87 | } else if (firstDay.getDay() < dayIndex) { |
| 88 | firstMatchingDay.setDate(firstMatchingDay.getDate() + (dayIndex - firstDay.getDay())); |
| 89 | } |
| 90 | |
| 91 | // For first week, we already have the date |
| 92 | if (week === 'first') { |
| 93 | return firstMatchingDay; |
| 94 | } |
| 95 | |
| 96 | // For other weeks, add appropriate number of weeks |
| 97 | const targetDate = new Date(firstMatchingDay); |
| 98 | targetDate.setDate(targetDate.getDate() + (weekIndex * 7)); |
| 99 | |
| 100 | // Edge Case: Verify the calculated date is still in the correct month |
| 101 | if (targetDate.getMonth() !== month - 1) { |
| 102 | throw new Error(`No ${week} ${dayOfWeek} in the given month`); |
| 103 | } |
| 104 | |
| 105 | return targetDate; |
| 106 | |
| 107 | // Handled Edge Cases: Invalid inputs, teenth week calculation, last week calculation, ordinal week calculation, date overflow to next month |
| 15 | 108 | }; |