| 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 | // Validate inputs |
| 21 | // Edge Case: Invalid month values |
| 22 | if (month < 1 || month > 12) { |
| 23 | throw new Error('Invalid month'); |
| 24 | } |
| 25 | |
| 26 | // Edge Case: Invalid year values |
| 27 | if (year < 1) { |
| 28 | throw new Error('Invalid year'); |
| 29 | } |
| 30 | |
| 31 | // Edge Case: Invalid week descriptor |
| 32 | const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth']; |
| 33 | if (!validWeeks.includes(week)) { |
| 34 | throw new Error('Invalid week descriptor'); |
| 35 | } |
| 36 | |
| 37 | // Edge Case: Invalid day of week |
| 38 | const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
| 39 | if (!validDays.includes(dayOfWeek)) { |
| 40 | throw new Error('Invalid day of week'); |
| 41 | } |
| 42 | |
| 43 | // Convert day of week to number (0-6) |
| 44 | const dayOfWeekNumber = validDays.indexOf(dayOfWeek); |
| 45 | |
| 46 | // Create a date object for the first day of the month |
| 47 | const firstDay = new Date(year, month - 1, 1); |
| 48 | |
| 49 | // Handle different week descriptors |
| 50 | switch (week) { |
| 51 | case 'first': |
| 52 | // Edge Case: First week - find the first occurrence of the day |
| 53 | return findNthOccurrence(firstDay, dayOfWeekNumber, 1); |
| 54 | |
| 55 | case 'second': |
| 56 | // Edge Case: Second week - find the second occurrence of the day |
| 57 | return findNthOccurrence(firstDay, dayOfWeekNumber, 2); |
| 58 | |
| 59 | case 'third': |
| 60 | // Edge Case: Third week - find the third occurrence of the day |
| 61 | return findNthOccurrence(firstDay, dayOfWeekNumber, 3); |
| 62 | |
| 63 | case 'fourth': |
| 64 | // Edge Case: Fourth week - find the fourth occurrence of the day |
| 65 | return findNthOccurrence(firstDay, dayOfWeekNumber, 4); |
| 66 | |
| 67 | case 'last': |
| 68 | // Edge Case: Last week - find the last occurrence of the day in the month |
| 69 | return findLastOccurrence(firstDay, dayOfWeekNumber); |
| 70 | |
| 71 | case 'teenth': |
| 72 | // Edge Case: Teenth week - find the occurrence of the day in the teenth range (13-19) |
| 73 | return findTeenthOccurrence(firstDay, dayOfWeekNumber); |
| 74 | |
| 75 | default: |
| 76 | throw new Error('Invalid week descriptor'); |
| 77 | } |
| 78 | |
| 79 | // Handled Edge Cases: Invalid month values, Invalid year values, Invalid week descriptor, Invalid day of week, First week, Second week, Third week, Fourth week, Last week, Teenth week |
| 15 | 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Finds the nth occurrence of a specific day of the week in a month |
| 84 | * |
| 85 | * @param {Date} firstDay - The first day of the month |
| 86 | * @param {number} dayOfWeek - The target day of the week (0-6) |
| 87 | * @param {number} occurrence - Which occurrence to find (1st, 2nd, etc.) |
| 88 | * @returns {Date} - The date of the nth occurrence |
| 89 | */ |
| 90 | function findNthOccurrence(firstDay, dayOfWeek, occurrence) { |
| 91 | // Find the first occurrence of the day |
| 92 | let date = new Date(firstDay); |
| 93 | |
| 94 | // If the first day is already the target day, we found the first occurrence |
| 95 | if (date.getDay() === dayOfWeek) { |
| 96 | // We already have the first occurrence |
| 97 | } else { |
| 98 | // Calculate days until the first occurrence of the target day |
| 99 | const daysUntil = (dayOfWeek - date.getDay() + 7) % 7; |
| 100 | date.setDate(date.getDate() + daysUntil); |
| 101 | } |
| 102 | |
| 103 | // If we want the first occurrence, we're done |
| 104 | if (occurrence === 1) { |
| 105 | return date; |
| 106 | } |
| 107 | |
| 108 | // Otherwise, add weeks to get to the nth occurrence |
| 109 | date.setDate(date.getDate() + (occurrence - 1) * 7); |
| 110 | |
| 111 | // Edge Case: Check if the calculated date is still in the same month |
| 112 | if (date.getMonth() !== firstDay.getMonth()) { |
| 113 | throw new Error('That day does not exist in that week'); |
| 114 | } |
| 115 | |
| 116 | return date; |
| 117 | // Handled Edge Cases: Date not in same month |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Finds the last occurrence of a specific day of the week in a month |
| 122 | * |
| 123 | * @param {Date} firstDay - The first day of the month |
| 124 | * @param {number} dayOfWeek - The target day of the week (0-6) |
| 125 | * @returns {Date} - The date of the last occurrence |
| 126 | */ |
| 127 | function findLastOccurrence(firstDay, dayOfWeek) { |
| 128 | // Get the first day of the next month |
| 129 | const nextMonth = new Date(firstDay.getFullYear(), firstDay.getMonth() + 1, 1); |
| 130 | |
| 131 | // Go back one day to get the last day of the target month |
| 132 | const lastDay = new Date(nextMonth.getFullYear(), nextMonth.getMonth(), nextMonth.getDate() - 1); |
| 133 | |
| 134 | // Find the last occurrence by working backwards |
| 135 | const daysSince = (lastDay.getDay() - dayOfWeek + 7) % 7; |
| 136 | const lastOccurrence = new Date(lastDay); |
| 137 | lastOccurrence.setDate(lastDay.getDate() - daysSince); |
| 138 | |
| 139 | return lastOccurrence; |
| 140 | // Handled Edge Cases: None |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Finds the occurrence of a specific day of the week in the teenth range (13-19) |
| 145 | * |
| 146 | * @param {Date} firstDay - The first day of the month |
| 147 | * @param {number} dayOfWeek - The target day of the week (0-6) |
| 148 | * @returns {Date} - The date of the teenth occurrence |
| 149 | */ |
| 150 | function findTeenthOccurrence(firstDay, dayOfWeek) { |
| 151 | // Start with the 13th of the month |
| 152 | const teenthStart = new Date(firstDay.getFullYear(), firstDay.getMonth(), 13); |
| 153 | |
| 154 | // Find the first occurrence of the day on or after the 13th |
| 155 | const daysUntil = (dayOfWeek - teenthStart.getDay() + 7) % 7; |
| 156 | const teenthDate = new Date(teenthStart); |
| 157 | teenthDate.setDate(teenthStart.getDate() + daysUntil); |
| 158 | |
| 159 | return teenthDate; |
| 160 | // Handled Edge Cases: None |
| 161 | } |