| 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'); |
| 15 | | }; |
| 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 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 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 | // Iterate backwards from the last day until we find the correct day of week |
| 51 | let currentDay = lastDay.getDate(); |
| 52 | while (currentDay >= 1) { |
| 53 | const testDate = new Date(year, month - 1, currentDay); |
| 54 | if (testDate.getDay() === dayOfWeekNumber) { |
| 55 | return testDate; |
| 56 | } |
| 57 | currentDay--; |
| 58 | } |
| 59 | |
| 60 | // This should never happen with valid inputs |
| 61 | throw new Error('Could not find last occurrence of the specified day'); |
| 62 | } |
| 63 | |
| 64 | // Edge Case: Handle 'teenth' week (days 13-19) |
| 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() === dayOfWeekNumber) { |
| 69 | return testDate; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // This should never happen with valid inputs |
| 74 | throw new Error('Could not find teenth occurrence of the specified day'); |
| 75 | } |
| 76 | |
| 77 | // Handle first, second, third, fourth weeks |
| 78 | const weekNumber = ['first', 'second', 'third', 'fourth'].indexOf(week) + 1; |
| 79 | |
| 80 | // Find the first occurrence of the day |
| 81 | let date = 1; |
| 82 | let occurrences = 0; |
| 83 | |
| 84 | // Get the number of days in the month |
| 85 | const daysInMonth = new Date(year, month, 0).getDate(); |
| 86 | |
| 87 | // Edge Case: Iterate through all days of the month to find occurrences |
| 88 | while (date <= daysInMonth) { |
| 89 | const testDate = new Date(year, month - 1, date); |
| 90 | if (testDate.getDay() === dayOfWeekNumber) { |
| 91 | occurrences++; |
| 92 | if (occurrences === weekNumber) { |
| 93 | return testDate; |
| 94 | } |
| 95 | } |
| 96 | date++; |
| 97 | } |
| 98 | |
| 99 | // Edge Case: Requested week number doesn't exist in the month |
| 100 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`); |
| 101 | |
| 102 | // Handled Edge Cases: |
| 103 | // 1. Invalid year input |
| 104 | // 2. Invalid month input |
| 105 | // 3. Invalid week descriptor |
| 106 | // 4. Invalid day of week |
| 107 | // 5. Last week of month calculation |
| 108 | // 6. Teenth week calculation (days 13-19) |
| 109 | // 7. Requested week occurrence doesn't exist in month |
| 110 | // 8. General iteration through month days |
| 111 | }; |