| 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 exact 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 | // Edge Case: Invalid month values |
| 21 | if (month < 1 || month > 12) { |
| 22 | throw new Error('Invalid month'); |
| 23 | } |
| 24 | |
| 25 | // Edge Case: Invalid year values |
| 26 | if (year < 1) { |
| 27 | throw new Error('Invalid year'); |
| 28 | } |
| 29 | |
| 30 | // Map day names to their corresponding index (0-6) |
| 31 | const days = { |
| 32 | 'Sunday': 0, |
| 33 | 'Monday': 1, |
| 34 | 'Tuesday': 2, |
| 35 | 'Wednesday': 3, |
| 36 | 'Thursday': 4, |
| 37 | 'Friday': 5, |
| 38 | 'Saturday': 6 |
| 39 | }; |
| 40 | |
| 41 | // Edge Case: Invalid day of week |
| 42 | if (!(dayOfWeek in days)) { |
| 43 | throw new Error('Invalid day of week'); |
| 44 | } |
| 45 | |
| 46 | const targetDay = days[dayOfWeek]; |
| 47 | |
| 48 | // Edge Case: Invalid week descriptor |
| 49 | const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth']; |
| 50 | if (!validWeeks.includes(week)) { |
| 51 | throw new Error('Invalid week descriptor'); |
| 52 | } |
| 53 | |
| 54 | // Create a date object for the first day of the month |
| 55 | const firstDay = new Date(year, month - 1, 1); |
| 56 | |
| 57 | // For 'teenth' week, we look for days 13-19 |
| 58 | if (week === 'teenth') { |
| 59 | // Edge Case: No matching date in teenth range (should not happen with valid inputs) |
| 60 | for (let day = 13; day <= 19; day++) { |
| 61 | const date = new Date(year, month - 1, day); |
| 62 | if (date.getDay() === targetDay) { |
| 63 | return date; |
| 64 | } |
| 65 | } |
| 66 | throw new Error('No matching date found'); |
| 67 | } |
| 68 | |
| 69 | // For 'last' week, we start from the end of the month |
| 70 | if (week === 'last') { |
| 71 | // Get the last day of the month |
| 72 | const lastDay = new Date(year, month, 0); |
| 73 | const daysInMonth = lastDay.getDate(); |
| 74 | |
| 75 | // Edge Case: No matching date in last week (should not happen with valid inputs) |
| 76 | for (let day = daysInMonth; day >= 1; day--) { |
| 77 | const date = new Date(year, month - 1, day); |
| 78 | if (date.getDay() === targetDay) { |
| 79 | return date; |
| 80 | } |
| 81 | } |
| 82 | throw new Error('No matching date found'); |
| 83 | } |
| 84 | |
| 85 | // For first, second, third, fourth, we count occurrences |
| 86 | const weekNumbers = { |
| 87 | 'first': 1, |
| 88 | 'second': 2, |
| 89 | 'third': 3, |
| 90 | 'fourth': 4 |
| 91 | }; |
| 92 | |
| 93 | if (week in weekNumbers) { |
| 94 | const occurrence = weekNumbers[week]; |
| 95 | let count = 0; |
| 96 | |
| 97 | // Get the number of days in the month |
| 98 | const daysInMonth = new Date(year, month, 0).getDate(); |
| 99 | |
| 100 | // Edge Case: Requesting a week that doesn't exist in the month (e.g., fifth Friday in a month with only 4 Fridays) |
| 101 | for (let day = 1; day <= daysInMonth; day++) { |
| 102 | const date = new Date(year, month - 1, day); |
| 103 | if (date.getDay() === targetDay) { |
| 104 | count++; |
| 105 | if (count === occurrence) { |
| 106 | return date; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // If we get here, the requested occurrence doesn't exist in the month |
| 112 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`); |
| 113 | } |
| 114 | |
| 115 | // This should never be reached due to validation at the start |
| 116 | throw new Error('Invalid week descriptor'); |
| 15 | 117 | }; |
| 118 | |
| 119 | // Handled Edge Cases: Invalid month values, Invalid year values, Invalid day of week, Invalid week descriptor, No matching date in teenth range, No matching date in last week, Requesting a week that doesn't exist in the month |