| 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 parameters |
| 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 | * |
| 14 | * Handled Edge Cases: |
| 15 | * - Invalid month values (outside 1-12) |
| 16 | * - Invalid week descriptors |
| 17 | * - Invalid day of week names |
| 18 | * - Non-existent dates (e.g., 30th of February) |
| 19 | * - Last week calculations when month has fewer occurrences of a day |
| 20 | * - Teenth week calculations |
| 12 | 21 | */ |
| 13 | 22 | export const meetup = (year, month, week, dayOfWeek) => { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 23 | // Validate inputs |
| 24 | if (month < 1 || month > 12) { |
| 25 | throw new Error('Invalid month'); |
| 26 | } |
| 27 | |
| 28 | // Map day names to numbers (0 = Sunday, 1 = Monday, ..., 6 = Saturday) |
| 29 | const days = { |
| 30 | 'Sunday': 0, |
| 31 | 'Monday': 1, |
| 32 | 'Tuesday': 2, |
| 33 | 'Wednesday': 3, |
| 34 | 'Thursday': 4, |
| 35 | 'Friday': 5, |
| 36 | 'Saturday': 6 |
| 37 | }; |
| 38 | |
| 39 | // Edge Case: Invalid day of week name |
| 40 | if (!(dayOfWeek in days)) { |
| 41 | throw new Error('Invalid day of week'); |
| 42 | } |
| 43 | |
| 44 | const targetDay = days[dayOfWeek]; |
| 45 | |
| 46 | // Edge Case: Invalid week descriptor |
| 47 | const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth']; |
| 48 | if (!validWeeks.includes(week)) { |
| 49 | throw new Error('Invalid week descriptor'); |
| 50 | } |
| 51 | |
| 52 | // Create a date object for the first day of the month |
| 53 | // Note: JavaScript months are 0-indexed, so we subtract 1 from the month |
| 54 | const firstDay = new Date(year, month - 1, 1); |
| 55 | |
| 56 | // Edge Case: Non-existent dates (e.g., 30th of February) |
| 57 | if (firstDay.getMonth() !== month - 1) { |
| 58 | throw new Error('Invalid date'); |
| 59 | } |
| 60 | |
| 61 | if (week === 'teenth') { |
| 62 | // For teenth, we look for the day in the range 13-19 |
| 63 | // Edge Case: Teenth week calculations |
| 64 | for (let day = 13; day <= 19; day++) { |
| 65 | const date = new Date(year, month - 1, day); |
| 66 | if (date.getDay() === targetDay) { |
| 67 | return date; |
| 68 | } |
| 69 | } |
| 70 | } else if (week === 'last') { |
| 71 | // For last, we start from the last day of the month and work backwards |
| 72 | // Edge Case: Last week calculations when month has fewer occurrences of a day |
| 73 | const lastDay = new Date(year, month, 0); // 0 gives us the last day of the previous month |
| 74 | let date = lastDay.getDate(); |
| 75 | |
| 76 | while (date >= 1) { |
| 77 | const currentDate = new Date(year, month - 1, date); |
| 78 | if (currentDate.getDay() === targetDay) { |
| 79 | return currentDate; |
| 80 | } |
| 81 | date--; |
| 82 | } |
| 83 | } else { |
| 84 | // For first, second, third, fourth |
| 85 | const weekNumbers = { |
| 86 | 'first': 1, |
| 87 | 'second': 2, |
| 88 | 'third': 3, |
| 89 | 'fourth': 4 |
| 90 | }; |
| 91 | |
| 92 | const weekNumber = weekNumbers[week]; |
| 93 | |
| 94 | // Find the first occurrence of the target day |
| 95 | const firstDayOfWeek = firstDay.getDay(); |
| 96 | let daysUntilTarget = targetDay - firstDayOfWeek; |
| 97 | |
| 98 | // If the target day has already passed in the first week, move to next week |
| 99 | if (daysUntilTarget < 0) { |
| 100 | daysUntilTarget += 7; |
| 101 | } |
| 102 | |
| 103 | // Calculate the date of the first occurrence |
| 104 | const firstOccurrence = 1 + daysUntilTarget; |
| 105 | |
| 106 | // Calculate the date of the requested occurrence |
| 107 | const targetDate = firstOccurrence + (weekNumber - 1) * 7; |
| 108 | |
| 109 | // Create the date object |
| 110 | const result = new Date(year, month - 1, targetDate); |
| 111 | |
| 112 | // Edge Case: Check if the calculated date is still in the correct month |
| 113 | if (result.getMonth() !== month - 1) { |
| 114 | throw new Error('Invalid date'); |
| 115 | } |
| 116 | |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | // This should never be reached if the logic above is correct |
| 121 | throw new Error('Could not determine meetup date'); |
| 15 | 122 | }; |