| 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, 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 week ('Sunday', 'Monday', etc.) |
| 13 | * @returns {Date} - The calculated date of the meetup |
| 12 | 14 | */ |
| 13 | 15 | export const meetup = (year, month, week, dayOfWeek) => { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 16 | // Validate inputs |
| 17 | if (typeof year !== 'number' || year < 1) { |
| 18 | throw new Error('Invalid year'); |
| 19 | } |
| 20 | |
| 21 | if (typeof month !== 'number' || month < 1 || month > 12) { |
| 22 | throw new Error('Invalid month'); |
| 23 | } |
| 24 | |
| 25 | const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth']; |
| 26 | if (!validWeeks.includes(week)) { |
| 27 | throw new Error('Invalid week'); |
| 28 | } |
| 29 | |
| 30 | const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
| 31 | if (!validDays.includes(dayOfWeek)) { |
| 32 | throw new Error('Invalid day of week'); |
| 33 | } |
| 34 | |
| 35 | // Convert day of week to number (0-6) |
| 36 | const targetDay = validDays.indexOf(dayOfWeek); |
| 37 | |
| 38 | // Create a date object for the first day of the month |
| 39 | const firstDay = new Date(year, month - 1, 1); |
| 40 | |
| 41 | // Edge Case: Handle 'first' week |
| 42 | if (week === 'first') { |
| 43 | const firstDayOfWeek = firstDay.getDay(); |
| 44 | const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7; |
| 45 | return new Date(year, month - 1, 1 + daysToAdd); |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Handle 'second' week |
| 49 | if (week === 'second') { |
| 50 | const firstDayOfWeek = firstDay.getDay(); |
| 51 | const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7 + 7; |
| 52 | return new Date(year, month - 1, 1 + daysToAdd); |
| 53 | } |
| 54 | |
| 55 | // Edge Case: Handle 'third' week |
| 56 | if (week === 'third') { |
| 57 | const firstDayOfWeek = firstDay.getDay(); |
| 58 | const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7 + 14; |
| 59 | return new Date(year, month - 1, 1 + daysToAdd); |
| 60 | } |
| 61 | |
| 62 | // Edge Case: Handle 'fourth' week |
| 63 | if (week === 'fourth') { |
| 64 | const firstDayOfWeek = firstDay.getDay(); |
| 65 | const daysToAdd = (targetDay - firstDayOfWeek + 7) % 7 + 21; |
| 66 | return new Date(year, month - 1, 1 + daysToAdd); |
| 67 | } |
| 68 | |
| 69 | // Edge Case: Handle 'last' week |
| 70 | if (week === 'last') { |
| 71 | // Get the first day of the next month |
| 72 | const nextMonth = new Date(year, month, 1); |
| 73 | // Get the last day of the target month |
| 74 | const lastDay = new Date(nextMonth.getTime() - 86400000); |
| 75 | const lastDayOfWeek = lastDay.getDay(); |
| 76 | |
| 77 | // Calculate days to subtract to get to the target day |
| 78 | const daysToSubtract = (lastDayOfWeek - targetDay + 7) % 7; |
| 79 | |
| 80 | return new Date(lastDay.getFullYear(), lastDay.getMonth(), lastDay.getDate() - daysToSubtract); |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Handle 'teenth' week |
| 84 | if (week === 'teenth') { |
| 85 | // Teenth days are 13-19 |
| 86 | for (let day = 13; day <= 19; day++) { |
| 87 | const date = new Date(year, month - 1, day); |
| 88 | if (date.getDay() === targetDay) { |
| 89 | return date; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // This should never be reached due to input validation |
| 95 | throw new Error('Unable to calculate meetup date'); |
| 96 | |
| 97 | // Handled Edge Cases: Invalid year, invalid month, invalid week descriptor, invalid day of week, |
| 98 | // first week of month, second week of month, third week of month, fourth week of month, |
| 99 | // last week of month, teenth days (13-19) of month |
| 15 | 100 | }; |