| 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 | /* |
| 2 | * Meetup exercise implementation |
| 3 | * Finds the exact date of a meetup given year, month, week, and day of week |
| 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 the given criteria |
| 8 | * @param {number} year - The year of the meetup |
| 9 | * @param {number} month - The month of the meetup (1-12) |
| 10 | * @param {string} week - The week descriptor ('first', 'second', 'third', 'fourth', 'last', 'teenth') |
| 11 | * @param {string} dayOfWeek - The day of the week ('Sunday', 'Monday', etc.) |
| 12 | * @returns {Date} - The calculated date of the meetup |
| 13 | * @throws {Error} - If the inputs are invalid or no matching date is found |
| 12 | 14 | */ |
| 13 | 15 | export const meetup = (year, month, week, dayOfWeek) => { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 15 | | }; |
| 16 | // Validate inputs |
| 17 | if (!Number.isInteger(year) || year < 1) { |
| 18 | throw new Error('Invalid year'); |
| 19 | } |
| 20 | |
| 21 | if (!Number.isInteger(month) || 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 descriptor'); |
| 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 | // Get the target day of week as a 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 'teenth' week (days 13-19) |
| 42 | if (week === 'teenth') { |
| 43 | // Start checking from the 13th day of the month |
| 44 | for (let day = 13; day <= 19; day++) { |
| 45 | const date = new Date(year, month - 1, day); |
| 46 | if (date.getDay() === targetDay) { |
| 47 | return date; |
| 48 | } |
| 49 | } |
| 50 | // This should never happen with valid inputs |
| 51 | throw new Error('No matching date found for teenth week'); |
| 52 | } |
| 53 | |
| 54 | // Edge Case: Handle 'last' week |
| 55 | if (week === 'last') { |
| 56 | // Get the last day of the month |
| 57 | const lastDay = new Date(year, month, 0); |
| 58 | const daysInMonth = lastDay.getDate(); |
| 59 | |
| 60 | // Work backwards from the last day to find the last occurrence of the target day |
| 61 | for (let day = daysInMonth; day >= 1; day--) { |
| 62 | const date = new Date(year, month - 1, day); |
| 63 | if (date.getDay() === targetDay) { |
| 64 | return date; |
| 65 | } |
| 66 | } |
| 67 | // This should never happen with valid inputs |
| 68 | throw new Error('No matching date found for last week'); |
| 69 | } |
| 70 | |
| 71 | // Handle 'first', 'second', 'third', 'fourth' weeks |
| 72 | const weekNumbers = { |
| 73 | 'first': 1, |
| 74 | 'second': 2, |
| 75 | 'third': 3, |
| 76 | 'fourth': 4 |
| 77 | }; |
| 78 | |
| 79 | const weekNumber = weekNumbers[week]; |
| 80 | let occurrenceCount = 0; |
| 81 | |
| 82 | // Get the number of days in the month |
| 83 | const daysInMonth = new Date(year, month, 0).getDate(); |
| 84 | |
| 85 | // Iterate through each day of the month |
| 86 | for (let day = 1; day <= daysInMonth; day++) { |
| 87 | const date = new Date(year, month - 1, day); |
| 88 | |
| 89 | // Check if this day matches the target day of week |
| 90 | if (date.getDay() === targetDay) { |
| 91 | occurrenceCount++; |
| 92 | |
| 93 | // If this is the nth occurrence we're looking for, return this date |
| 94 | if (occurrenceCount === weekNumber) { |
| 95 | return date; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Edge Case: Handle case where the requested week doesn't exist in the month |
| 101 | throw new Error(`No matching date found for ${week} ${dayOfWeek}`); |
| 102 | |
| 103 | // Handled Edge Cases: |
| 104 | // 1. Invalid year (non-integer or less than 1) |
| 105 | // 2. Invalid month (non-integer or not in range 1-12) |
| 106 | // 3. Invalid week descriptor (not one of the valid options) |
| 107 | // 4. Invalid day of week (not one of the valid day names) |
| 108 | // 5. No matching date found for teenth week |
| 109 | // 6. No matching date found for last week |
| 110 | // 7. Requested week doesn't exist in the month (e.g., fifth Monday in a month that only has four Mondays) |
| 111 | }; |