| 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'); |
| 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'); |
| 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 = Sunday, 1 = Monday, ..., 6 = Saturday) |
| 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 'first' week |
| 46 | if (week === 'first') { |
| 47 | // Find the first occurrence of the specified day |
| 48 | const dayOffset = (dayOfWeekNumber - firstDay.getDay() + 7) % 7; |
| 49 | return new Date(year, month - 1, 1 + dayOffset); |
| 50 | } |
| 51 | |
| 52 | // Edge Case: Handle 'second' week |
| 53 | if (week === 'second') { |
| 54 | // Find the first occurrence and add 7 days |
| 55 | const firstOccurrence = new Date(firstDay); |
| 56 | const dayOffset = (dayOfWeekNumber - firstDay.getDay() + 7) % 7; |
| 57 | firstOccurrence.setDate(1 + dayOffset); |
| 58 | firstOccurrence.setDate(firstOccurrence.getDate() + 7); |
| 59 | return firstOccurrence; |
| 60 | } |
| 61 | |
| 62 | // Edge Case: Handle 'third' week |
| 63 | if (week === 'third') { |
| 64 | // Find the first occurrence and add 14 days |
| 65 | const firstOccurrence = new Date(firstDay); |
| 66 | const dayOffset = (dayOfWeekNumber - firstDay.getDay() + 7) % 7; |
| 67 | firstOccurrence.setDate(1 + dayOffset); |
| 68 | firstOccurrence.setDate(firstOccurrence.getDate() + 14); |
| 69 | return firstOccurrence; |
| 70 | } |
| 71 | |
| 72 | // Edge Case: Handle 'fourth' week |
| 73 | if (week === 'fourth') { |
| 74 | // Find the first occurrence and add 21 days |
| 75 | const firstOccurrence = new Date(firstDay); |
| 76 | const dayOffset = (dayOfWeekNumber - firstDay.getDay() + 7) % 7; |
| 77 | firstOccurrence.setDate(1 + dayOffset); |
| 78 | firstOccurrence.setDate(firstOccurrence.getDate() + 21); |
| 79 | return firstOccurrence; |
| 80 | } |
| 81 | |
| 82 | // Edge Case: Handle 'last' week |
| 83 | if (week === 'last') { |
| 84 | // Find the last day of the month |
| 85 | const lastDay = new Date(year, month, 0); // 0 gives us the last day of the previous month |
| 86 | |
| 87 | // Find the last occurrence of the specified day |
| 88 | const dayOffset = (lastDay.getDay() - dayOfWeekNumber + 7) % 7; |
| 89 | return new Date(year, month - 1, lastDay.getDate() - dayOffset); |
| 90 | } |
| 91 | |
| 92 | // Edge Case: Handle 'teenth' week |
| 93 | if (week === 'teenth') { |
| 94 | // The teenth days are 13-19 |
| 95 | for (let day = 13; day <= 19; day++) { |
| 96 | const date = new Date(year, month - 1, day); |
| 97 | if (date.getDay() === dayOfWeekNumber) { |
| 98 | return date; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // This should never be reached due to input validation |
| 104 | throw new Error('Unable to determine meetup date'); |
| 105 | |
| 106 | // Handled Edge Cases: Invalid year, invalid month, invalid week descriptor, invalid day of week, |
| 107 | // first week, second week, third week, fourth week, last week, teenth week |
| 108 | }; |