Immune

Finished
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 solution
3 * Finds the exact date of a meetup given month, year, weekday and week
4 */
55
66/**
7 * @param {number} year
8 * @param {number} month
9 * @param {string} week
10 * @param {string} dayOfWeek
11 * @returns {Date}
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 the week ('Sunday', 'Monday', etc.)
11 * @returns {Date} - The date of the meetup
1212 */
1313export const meetup = (year, month, week, dayOfWeek) => {
14 throw new Error('Remove this line and implement the function');
15};
14 // Validate inputs
15 if (typeof year !== 'number' || year < 1) {
16 throw new Error('Invalid year');
17 }
18
19 if (typeof month !== 'number' || month < 1 || month > 12) {
20 throw new Error('Invalid month');
21 }
22
23 const validWeeks = ['first', 'second', 'third', 'fourth', 'last', 'teenth'];
24 if (!validWeeks.includes(week)) {
25 throw new Error('Invalid week');
26 }
27
28 const validDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
29 if (!validDays.includes(dayOfWeek)) {
30 throw new Error('Invalid day of week');
31 }
32
33 // Get the target day of week as a number (0-6)
34 const targetDayNum = validDays.indexOf(dayOfWeek);
35
36 // Get the number of days in the month
37 const daysInMonth = new Date(year, month, 0).getDate();
38
39 // Edge Case: Handle 'teenth' week (days 13-19)
40 if (week === 'teenth') {
41 for (let day = 13; day <= 19; day++) {
42 const date = new Date(year, month - 1, day);
43 if (date.getDay() === targetDayNum) {
44 return date;
45 }
46 }
47 }
48
49 // Edge Case: Handle 'last' week
50 if (week === 'last') {
51 // Start from the last day of the month and work backwards
52 for (let day = daysInMonth; day >= 1; day--) {
53 const date = new Date(year, month - 1, day);
54 if (date.getDay() === targetDayNum) {
55 return date;
56 }
57 }
58 }
59
60 // Edge Case: Handle 'first', 'second', 'third', 'fourth' weeks
61 let occurrenceCount = 0;
62 const targetOccurrence = ['first', 'second', 'third', 'fourth'].indexOf(week) + 1;
63
64 for (let day = 1; day <= daysInMonth; day++) {
65 const date = new Date(year, month - 1, day);
66 if (date.getDay() === targetDayNum) {
67 occurrenceCount++;
68 if (occurrenceCount === targetOccurrence) {
69 return date;
70 }
71 }
72 }
73
74 // Edge Case: If the requested occurrence doesn't exist in the month
75 throw new Error('No such occurrence of the specified day in the given month');
76
77 // Handled Edge Cases: Invalid inputs, teenth week, last week, first-fourth weeks, non-existent occurrences
78};
Test NameStatus
monteenth of May 2013
Pass
monteenth of August 2013
Pass
monteenth of September 2013
Pass
tuesteenth of March 2013
Pass
tuesteenth of April 2013
Pass
tuesteenth of August 2013
Pass
wednesteenth of January 2013
Pass
wednesteenth of February 2013
Pass
wednesteenth of June 2013
Pass
thursteenth of May 2013
Pass
thursteenth of June 2013
Pass
thursteenth of September 2013
Pass
friteenth of April 2013
Pass
friteenth of August 2013
Pass
friteenth of September 2013
Pass
saturteenth of February 2013
Pass
saturteenth of April 2013
Pass
saturteenth of October 2013
Pass
sunteenth of May 2013
Pass
sunteenth of June 2013
Pass
sunteenth of October 2013
Pass
first Monday of March 2013
Pass
first Monday of April 2013
Pass
first Tuesday of May 2013
Pass
first Tuesday of June 2013
Pass
first Wednesday of July 2013
Pass
first Wednesday of August 2013
Pass
first Thursday of September 2013
Pass
first Thursday of October 2013
Pass
first Friday of November 2013
Pass
first Friday of December 2013
Pass
first Saturday of January 2013
Pass
first Saturday of February 2013
Pass
first Sunday of March 2013
Pass
first Sunday of April 2013
Pass
second Monday of March 2013
Pass
second Monday of April 2013
Pass
second Tuesday of May 2013
Pass
second Tuesday of June 2013
Pass
second Wednesday of July 2013
Pass
second Wednesday of August 2013
Pass
second Thursday of September 2013
Pass
second Thursday of October 2013
Pass
second Friday of November 2013
Pass
second Friday of December 2013
Pass
second Saturday of January 2013
Pass
second Saturday of February 2013
Pass
second Sunday of March 2013
Pass
second Sunday of April 2013
Pass
third Monday of March 2013
Pass
third Monday of April 2013
Pass
third Tuesday of May 2013
Pass
third Tuesday of June 2013
Pass
third Wednesday of July 2013
Pass
third Wednesday of August 2013
Pass
third Thursday of September 2013
Pass
third Thursday of October 2013
Pass
third Friday of November 2013
Pass
third Friday of December 2013
Pass
third Saturday of January 2013
Pass
third Saturday of February 2013
Pass
third Sunday of March 2013
Pass
third Sunday of April 2013
Pass
fourth Monday of March 2013
Pass
fourth Monday of April 2013
Pass
fourth Tuesday of May 2013
Pass
fourth Tuesday of June 2013
Pass
fourth Wednesday of July 2013
Pass
fourth Wednesday of August 2013
Pass
fourth Thursday of September 2013
Pass
fourth Thursday of October 2013
Pass
fourth Friday of November 2013
Pass
fourth Friday of December 2013
Pass
fourth Saturday of January 2013
Pass
fourth Saturday of February 2013
Pass
fourth Sunday of March 2013
Pass
fourth Sunday of April 2013
Pass
last Monday of March 2013
Pass
last Monday of April 2013
Pass
last Tuesday of May 2013
Pass
last Tuesday of June 2013
Pass
last Wednesday of July 2013
Pass
last Wednesday of August 2013
Pass
last Thursday of September 2013
Pass
last Thursday of October 2013
Pass
last Friday of November 2013
Pass
last Friday of December 2013
Pass
last Saturday of January 2013
Pass
last Saturday of February 2013
Pass
last Sunday of March 2013
Pass
last Sunday of April 2013
Pass
last Wednesday of February 2012
Pass
last Wednesday of December 2014
Pass
last Sunday of February 2015
Pass
first Friday of December 2012
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.