| 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 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 week ('Sunday', 'Monday', etc.) |
| 13 | * @returns {Date} - The calculated date of the meetup |
| 14 | * |
| 15 | * Handled Edge Cases: |
| 16 | * - Invalid month values (less than 1 or greater than 12) |
| 17 | * - Invalid week descriptors |
| 18 | * - Invalid day of week names |
| 19 | * - No matching date found for the given criteria |
| 12 | 20 | */ |
| 13 | 21 | export const meetup = (year, month, week, dayOfWeek) => { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Edge Case: Invalid month values |
| 23 | if (month < 1 || month > 12) { |
| 24 | throw new Error('Invalid month'); |
| 25 | } |
| 26 | |
| 27 | // Map day names to their corresponding index (0-6) |
| 28 | const days = { |
| 29 | 'Sunday': 0, |
| 30 | 'Monday': 1, |
| 31 | 'Tuesday': 2, |
| 32 | 'Wednesday': 3, |
| 33 | 'Thursday': 4, |
| 34 | 'Friday': 5, |
| 35 | 'Saturday': 6 |
| 36 | }; |
| 37 | |
| 38 | // Edge Case: Invalid day of week names |
| 39 | if (!days.hasOwnProperty(dayOfWeek)) { |
| 40 | throw new Error('Invalid day of week'); |
| 41 | } |
| 42 | |
| 43 | const targetDay = days[dayOfWeek]; |
| 44 | |
| 45 | // Edge Case: Invalid week descriptors |
| 46 | if (!['first', 'second', 'third', 'fourth', 'last', 'teenth'].includes(week)) { |
| 47 | throw new Error('Invalid week descriptor'); |
| 48 | } |
| 49 | |
| 50 | // Create a date object for the first day of the month |
| 51 | const firstDay = new Date(year, month - 1, 1); |
| 52 | |
| 53 | // For 'last' week, we start from the last day of the month |
| 54 | if (week === 'last') { |
| 55 | // Get the last day of the month |
| 56 | const lastDay = new Date(year, month, 0); |
| 57 | |
| 58 | // Find the last occurrence of the target day |
| 59 | const diff = lastDay.getDay() - targetDay; |
| 60 | const daysAgo = diff >= 0 ? diff : 7 + diff; |
| 61 | return new Date(lastDay.getFullYear(), lastDay.getMonth(), lastDay.getDate() - daysAgo); |
| 62 | } |
| 63 | |
| 64 | // For 'teenth' week, we check days 13-19 |
| 65 | if (week === 'teenth') { |
| 66 | for (let day = 13; day <= 19; day++) { |
| 67 | const date = new Date(year, month - 1, day); |
| 68 | if (date.getDay() === targetDay) { |
| 69 | return date; |
| 70 | } |
| 71 | } |
| 72 | // Edge Case: No matching date found for the given criteria |
| 73 | throw new Error('No matching date found'); |
| 74 | } |
| 75 | |
| 76 | // For first, second, third, fourth, we count occurrences |
| 77 | const weekNumbers = { |
| 78 | 'first': 1, |
| 79 | 'second': 2, |
| 80 | 'third': 3, |
| 81 | 'fourth': 4 |
| 82 | }; |
| 83 | |
| 84 | if (weekNumbers.hasOwnProperty(week)) { |
| 85 | const occurrence = weekNumbers[week]; |
| 86 | let count = 0; |
| 87 | |
| 88 | // Start from the first day of the month and iterate through days |
| 89 | const date = new Date(firstDay); |
| 90 | |
| 91 | // Find the first occurrence of the target day |
| 92 | const diff = targetDay - firstDay.getDay(); |
| 93 | const daysAhead = diff >= 0 ? diff : 7 + diff; |
| 94 | date.setDate(firstDay.getDate() + daysAhead); |
| 95 | |
| 96 | // If the first occurrence is in the next month, there's no valid date |
| 97 | if (date.getMonth() !== firstDay.getMonth()) { |
| 98 | // Edge Case: No matching date found for the given criteria |
| 99 | throw new Error('No matching date found'); |
| 100 | } |
| 101 | |
| 102 | count++; |
| 103 | |
| 104 | // If this is the occurrence we're looking for, return it |
| 105 | if (count === occurrence) { |
| 106 | return date; |
| 107 | } |
| 108 | |
| 109 | // Otherwise, continue looking for the next occurrences |
| 110 | while (count < occurrence) { |
| 111 | date.setDate(date.getDate() + 7); |
| 112 | count++; |
| 113 | |
| 114 | // If we've moved to the next month, there's no valid date |
| 115 | if (date.getMonth() !== firstDay.getMonth()) { |
| 116 | // Edge Case: No matching date found for the given criteria |
| 117 | throw new Error('No matching date found'); |
| 118 | } |
| 119 | |
| 120 | if (count === occurrence) { |
| 121 | return date; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Edge Case: No matching date found for the given criteria |
| 127 | throw new Error('No matching date found'); |
| 15 | 128 | }; |