| 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 January 1, 2018 |
| 17 | * meetup(2020, 5, 'teenth', 'Wednesday') // Returns 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 descriptor'); |
| 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-6) |
| 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 'last' week descriptor |
| 46 | if (week === 'last') { |
| 47 | // Start from the last day of the month and work backwards |
| 48 | const lastDay = new Date(year, month, 0); // 0th day of next month = last day of current month |
| 49 | let currentDay = lastDay.getDate(); |
| 50 | |
| 51 | while (currentDay >= 1) { |
| 52 | const currentDate = new Date(year, month - 1, currentDay); |
| 53 | if (currentDate.getDay() === dayOfWeekNumber) { |
| 54 | return currentDate; |
| 55 | } |
| 56 | currentDay--; |
| 57 | } |
| 58 | |
| 59 | throw new Error('Could not find the last occurrence of the specified day'); |
| 60 | } |
| 61 | |
| 62 | // Edge Case: Handle 'teenth' week descriptor |
| 63 | if (week === 'teenth') { |
| 64 | // Teenth days are 13-19 |
| 65 | for (let day = 13; day <= 19; day++) { |
| 66 | const currentDate = new Date(year, month - 1, day); |
| 67 | if (currentDate.getDay() === dayOfWeekNumber) { |
| 68 | return currentDate; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | throw new Error('Could not find the teenth occurrence of the specified day'); |
| 73 | } |
| 74 | |
| 75 | // Handle first, second, third, fourth |
| 76 | const weekNumber = ['first', 'second', 'third', 'fourth'].indexOf(week) + 1; |
| 77 | |
| 78 | // Find the first occurrence of the day |
| 79 | let currentDate = new Date(firstDay); |
| 80 | |
| 81 | // If the first day of the month is already the target day, we found the first occurrence |
| 82 | // Otherwise, find the first occurrence |
| 83 | if (currentDate.getDay() !== dayOfWeekNumber) { |
| 84 | // Calculate days to add to reach the first occurrence |
| 85 | const daysToAdd = (dayOfWeekNumber - currentDate.getDay() + 7) % 7; |
| 86 | currentDate.setDate(currentDate.getDate() + daysToAdd); |
| 87 | } |
| 88 | |
| 89 | // Edge Case: Handle when the first occurrence is in the previous month |
| 90 | if (currentDate.getMonth() !== month - 1) { |
| 91 | currentDate.setDate(currentDate.getDate() + 7); |
| 92 | } |
| 93 | |
| 94 | // For subsequent occurrences, add 7 days (one week) for each occurrence |
| 95 | currentDate.setDate(currentDate.getDate() + (weekNumber - 1) * 7); |
| 96 | |
| 97 | // Edge Case: Handle when the calculated date is in the next month |
| 98 | if (currentDate.getMonth() !== month - 1) { |
| 99 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the specified month`); |
| 100 | } |
| 101 | |
| 102 | return currentDate; |
| 103 | |
| 104 | // Handled Edge Cases: |
| 105 | // 1. Invalid year (non-integer or less than 1) |
| 106 | // 2. Invalid month (non-integer or not in range 1-12) |
| 107 | // 3. Invalid week descriptor (not one of the valid options) |
| 108 | // 4. Invalid day of week (not one of the valid day names) |
| 109 | // 5. Last week descriptor (finding the last occurrence of a day in a month) |
| 110 | // 6. Teenth week descriptor (finding a day in the 13-19 range) |
| 111 | // 7. First occurrence in previous month (when first day of month is not the target day) |
| 112 | // 8. Requested occurrence does not exist in month (e.g. fifth Monday in a month with only 4 Mondays) |
| 15 | 113 | }; |