| 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 based on year, month, week descriptor, 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 | // Map day names to their index (0 = Sunday, 1 = Monday, ..., 6 = Saturday) |
| 21 | const days = { |
| 22 | 'Sunday': 0, |
| 23 | 'Monday': 1, |
| 24 | 'Tuesday': 2, |
| 25 | 'Wednesday': 3, |
| 26 | 'Thursday': 4, |
| 27 | 'Friday': 5, |
| 28 | 'Saturday': 6 |
| 29 | }; |
| 30 | |
| 31 | // Edge Case: Invalid day of week |
| 32 | if (!(dayOfWeek in days)) { |
| 33 | throw new Error('Invalid day of week'); |
| 34 | } |
| 35 | |
| 36 | const dayIndex = days[dayOfWeek]; |
| 37 | |
| 38 | // Create a date object for the first day of the given month |
| 39 | const firstDay = new Date(year, month - 1, 1); |
| 40 | |
| 41 | // Edge Case: Invalid month (JavaScript Date will wrap months, so we need to check) |
| 42 | if (firstDay.getMonth() !== month - 1) { |
| 43 | throw new Error('Invalid month'); |
| 44 | } |
| 45 | |
| 46 | // Edge Case: Invalid year (JavaScript Date handles most years, but we check for negative values) |
| 47 | if (year < 1) { |
| 48 | throw new Error('Invalid year'); |
| 49 | } |
| 50 | |
| 51 | // Find the first occurrence of the target day in the month |
| 52 | let firstOccurrence = firstDay.getDate() + (dayIndex - firstDay.getDay() + 7) % 7; |
| 53 | const firstDate = new Date(year, month - 1, firstOccurrence); |
| 54 | |
| 55 | switch (week) { |
| 56 | case 'first': |
| 57 | return firstDate; |
| 58 | |
| 59 | case 'second': |
| 60 | // Edge Case: Month doesn't have a second occurrence of the day |
| 61 | const secondDate = new Date(year, month - 1, firstOccurrence + 7); |
| 62 | // Check if we've moved to the next month |
| 63 | if (secondDate.getMonth() !== month - 1) { |
| 64 | throw new Error('Month does not have a second ' + dayOfWeek); |
| 65 | } |
| 66 | return secondDate; |
| 67 | |
| 68 | case 'third': |
| 69 | // Edge Case: Month doesn't have a third occurrence of the day |
| 70 | const thirdDate = new Date(year, month - 1, firstOccurrence + 14); |
| 71 | if (thirdDate.getMonth() !== month - 1) { |
| 72 | throw new Error('Month does not have a third ' + dayOfWeek); |
| 73 | } |
| 74 | return thirdDate; |
| 75 | |
| 76 | case 'fourth': |
| 77 | // Edge Case: Month doesn't have a fourth occurrence of the day |
| 78 | const fourthDate = new Date(year, month - 1, firstOccurrence + 21); |
| 79 | if (fourthDate.getMonth() !== month - 1) { |
| 80 | throw new Error('Month does not have a fourth ' + dayOfWeek); |
| 81 | } |
| 82 | return fourthDate; |
| 83 | |
| 84 | case 'last': |
| 85 | // Find the last occurrence by starting from the first occurrence |
| 86 | // and adding weeks until we go into the next month |
| 87 | let lastDate = new Date(year, month - 1, firstOccurrence); |
| 88 | let nextDate = new Date(year, month - 1, firstOccurrence + 7); |
| 89 | |
| 90 | while (nextDate.getMonth() === month - 1) { |
| 91 | lastDate = nextDate; |
| 92 | nextDate = new Date(year, month - 1, nextDate.getDate() + 7); |
| 93 | } |
| 94 | |
| 95 | return lastDate; |
| 96 | |
| 97 | case 'teenth': |
| 98 | // Find the teenth occurrence (13th through 19th) |
| 99 | // Start with the first occurrence and add weeks until we're in the teenth range |
| 100 | let teenthDate = new Date(year, month - 1, firstOccurrence); |
| 101 | |
| 102 | while (teenthDate.getDate() < 13) { |
| 103 | teenthDate = new Date(year, month - 1, teenthDate.getDate() + 7); |
| 104 | } |
| 105 | |
| 106 | // Edge Case: If we went past the 19th, there's no teenth occurrence |
| 107 | if (teenthDate.getDate() > 19) { |
| 108 | throw new Error('Month does not have a teenth ' + dayOfWeek); |
| 109 | } |
| 110 | |
| 111 | return teenthDate; |
| 112 | |
| 113 | default: |
| 114 | // Edge Case: Invalid week descriptor |
| 115 | throw new Error('Invalid week descriptor'); |
| 116 | } |
| 117 | |
| 118 | // Handled Edge Cases: Invalid day of week, invalid month, invalid year, month doesn't have required occurrence of day, invalid week descriptor |
| 15 | 119 | }; |