| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Meetup' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 1 | const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
| 2 | const weekDescriptors = ['first', 'second', 'third', 'fourth', 'last', 'teenth']; |
| 5 | 3 | |
| 6 | 4 | /** |
| 7 | | * @param {number} year |
| 8 | | * @param {number} month |
| 9 | | * @param {string} week |
| 10 | | * @param {string} dayOfWeek |
| 11 | | * @returns {Date} |
| 5 | * Calculates the date of a meetup based on year, month, week descriptor, and day of week. |
| 6 | * |
| 7 | * @param {number} year - The year of the meetup |
| 8 | * @param {number} month - The month of the meetup (1-12) |
| 9 | * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth') |
| 10 | * @param {string} dayOfWeek - The day of week ('Sunday', 'Monday', etc.) |
| 11 | * @returns {Date} - The calculated date of the meetup |
| 12 | * |
| 13 | * Edge Cases Handled: |
| 14 | * - Invalid month values (outside 1-12) |
| 15 | * - Invalid week descriptor |
| 16 | * - Invalid day of week |
| 17 | * - Non-integer year values |
| 18 | * - Last week calculation when month has fewer than 4 occurrences of a day |
| 19 | * - Teenth week calculation |
| 12 | 20 | */ |
| 13 | 21 | export const meetup = (year, month, week, dayOfWeek) => { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Validate inputs |
| 23 | |
| 24 | // Edge Case: Invalid month values (outside 1-12) |
| 25 | if (month < 1 || month > 12) { |
| 26 | throw new Error('Invalid month'); |
| 27 | } |
| 28 | |
| 29 | // Edge Case: Non-integer year values |
| 30 | if (!Number.isInteger(year)) { |
| 31 | throw new Error('Invalid year'); |
| 32 | } |
| 33 | |
| 34 | // Edge Case: Invalid week descriptor |
| 35 | if (!weekDescriptors.includes(week)) { |
| 36 | throw new Error('Invalid week descriptor'); |
| 37 | } |
| 38 | |
| 39 | // Edge Case: Invalid day of week |
| 40 | if (!weekdays.includes(dayOfWeek)) { |
| 41 | throw new Error('Invalid day of week'); |
| 42 | } |
| 43 | |
| 44 | const targetDayIndex = weekdays.indexOf(dayOfWeek); |
| 45 | |
| 46 | // Create a date object for the first day of the month |
| 47 | const firstDay = new Date(year, month - 1, 1); |
| 48 | |
| 49 | if (week === 'teenth') { |
| 50 | // For teenth, we look for the day in the range 13-19 |
| 51 | // Edge Case: Teenth week calculation |
| 52 | for (let day = 13; day <= 19; day++) { |
| 53 | const date = new Date(year, month - 1, day); |
| 54 | if (date.getDay() === targetDayIndex) { |
| 55 | return date; |
| 56 | } |
| 57 | } |
| 58 | } else if (week === 'last') { |
| 59 | // For last, we start from the last day of the month and work backwards |
| 60 | // Get the last day of the month |
| 61 | const lastDay = new Date(year, month, 0); |
| 62 | const daysInMonth = lastDay.getDate(); |
| 63 | |
| 64 | // Edge Case: Last week calculation when month has fewer than 4 occurrences of a day |
| 65 | for (let day = daysInMonth; day >= 1; day--) { |
| 66 | const date = new Date(year, month - 1, day); |
| 67 | if (date.getDay() === targetDayIndex) { |
| 68 | return date; |
| 69 | } |
| 70 | } |
| 71 | } else { |
| 72 | // For first, second, third, fourth |
| 73 | const weekIndex = weekDescriptors.indexOf(week); |
| 74 | let occurrenceCount = 0; |
| 75 | |
| 76 | // Get the number of days in the month |
| 77 | const daysInMonth = new Date(year, month, 0).getDate(); |
| 78 | |
| 79 | for (let day = 1; day <= daysInMonth; day++) { |
| 80 | const date = new Date(year, month - 1, day); |
| 81 | if (date.getDay() === targetDayIndex) { |
| 82 | occurrenceCount++; |
| 83 | if (occurrenceCount === weekIndex + 1) { |
| 84 | return date; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // If we get here, the requested occurrence doesn't exist in this month |
| 90 | throw new Error(`The ${week} ${dayOfWeek} does not exist in the given month`); |
| 91 | } |
| 92 | |
| 93 | // This should never be reached if the logic above is correct |
| 94 | throw new Error('Unable to determine meetup date'); |
| 15 | 95 | }; |
| 96 | |
| 97 | // Handled Edge Cases: Invalid month values (outside 1-12), Invalid week descriptor, Invalid day of week, Non-integer year values, Last week calculation when month has fewer than 4 occurrences of a day, Teenth week calculation |