| 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 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 'last' week by starting from the end of the month |
| 46 | if (week === 'last') { |
| 47 | // Get the last day of the month |
| 48 | const lastDay = new Date(year, month, 0); |
| 49 | |
| 50 | // Find the last occurrence of the specified day |
| 51 | let currentDay = lastDay.getDate(); |
| 52 | while (currentDay >= 1) { |
| 53 | const testDate = new Date(year, month - 1, currentDay); |
| 54 | if (testDate.getDay() === dayIndex) { |
| 55 | return testDate; |
| 56 | } |
| 57 | currentDay--; |
| 58 | } |
| 59 | |
| 60 | // This should never happen with valid inputs |
| 61 | throw new Error('Could not find the last occurrence of the specified day'); |
| 62 | } |
| 63 | |
| 64 | // Edge Case: Handle 'teenth' week (13th to 19th) |
| 65 | if (week === 'teenth') { |
| 66 | for (let day = 13; day <= 19; day++) { |
| 67 | const testDate = new Date(year, month - 1, day); |
| 68 | if (testDate.getDay() === dayIndex) { |
| 69 | return testDate; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // This should never happen with valid inputs |
| 74 | throw new Error('Could not find the teenth occurrence of the specified day'); |
| 75 | } |
| 76 | |
| 77 | // Handle first, second, third, fourth weeks |
| 78 | const weekIndex = validWeeks.indexOf(week); |
| 79 | |
| 80 | // Find the first occurrence of the specified day |
| 81 | let date = 1; |
| 82 | let firstOccurrence = -1; |
| 83 | |
| 84 | // Edge Case: Find the first occurrence of the specified day in the month |
| 85 | while (date <= 31) { |
| 86 | const testDate = new Date(year, month - 1, date); |
| 87 | // If we've moved to the next month, break |
| 88 | if (testDate.getMonth() !== month - 1) { |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | if (testDate.getDay() === dayIndex) { |
| 93 | firstOccurrence = date; |
| 94 | break; |
| 95 | } |
| 96 | date++; |
| 97 | } |
| 98 | |
| 99 | // Edge Case: If no occurrence found in the month |
| 100 | if (firstOccurrence === -1) { |
| 101 | throw new Error('Could not find the specified day in the month'); |
| 102 | } |
| 103 | |
| 104 | // Calculate the target date based on the week |
| 105 | const targetDate = firstOccurrence + (weekIndex * 7); |
| 106 | |
| 107 | // Edge Case: Validate that the target date is still in the same month |
| 108 | const result = new Date(year, month - 1, targetDate); |
| 109 | if (result.getMonth() !== month - 1) { |
| 110 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the specified month`); |
| 111 | } |
| 112 | |
| 113 | return result; |
| 114 | |
| 115 | // Handled Edge Cases: Invalid inputs, last week calculation, teenth week calculation, |
| 116 | // first occurrence finding, target date validation |
| 15 | 117 | }; |
| 118 | |
| 119 | // Handled Edge Cases: Invalid year, invalid month, invalid week descriptor, invalid day of week, |
| 120 | // last week calculation, teenth week calculation, first occurrence finding, |
| 121 | // target date validation, month boundary checks |