Skip to content

Commit 2b932d4

Browse files
committed
appointment-time.spec.js and appoint-time exemplar.js file ready
1 parent 028cbf0 commit 2b932d4

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,73 @@
11
// @ts-check
2+
3+
/**
4+
* Create an appointment
5+
*
6+
* @param {number} days
7+
*/
8+
export function createAppointment(days) {
9+
return new Date(Date.now() + days * 24 * 3600 * 1000);
10+
}
11+
12+
/**
13+
* Get details of an appointment
14+
*
15+
* @param {string} appointmentTime
16+
*/
17+
export function getAppointment(appointmentTime) {
18+
const appointmentDate = new Date(appointmentTime);
19+
return {
20+
year: appointmentDate.getFullYear(),
21+
month: appointmentDate.getMonth(),
22+
date: appointmentDate.getDate(),
23+
hour: appointmentDate.getHours(),
24+
minute: appointmentDate.getMinutes(),
25+
};
26+
}
27+
28+
/**
29+
* Update an appointment with given options
30+
*
31+
* @param {string} appointmentTime
32+
* @param {object} options
33+
*/
34+
export function updateAppointment(appointmentTime, options) {
35+
let appointmentDate = new Date(appointmentTime);
36+
if (options.year !== undefined) appointmentDate.setFullYear(options.year);
37+
if (options.month !== undefined) appointmentDate.setMonth(options.month);
38+
if (options.date !== undefined) appointmentDate.setDate(options.date);
39+
if (options.hour !== undefined) appointmentDate.setHours(options.hour);
40+
if (options.minute !== undefined) appointmentDate.setMinutes(options.minute);
41+
42+
return {
43+
year: appointmentDate.getFullYear(),
44+
month: appointmentDate.getMonth(),
45+
date: appointmentDate.getDate(),
46+
hour: appointmentDate.getHours(),
47+
minute: appointmentDate.getMinutes(),
48+
};
49+
}
50+
51+
/**
52+
* Get available times between two appointment
53+
*
54+
* @param {string} appointmentATime
55+
* @param {string} appointmentBTime
56+
*/
57+
export function availableTimes(appointmentATime, appointmentBTime) {
58+
return new Date(appointmentBTime) - new Date(appointmentATime);
59+
}
60+
61+
/**
62+
* Get available times between two appointment
63+
*
64+
* @param {string} appointmentTime
65+
* @param {string} currentTime
66+
*/
67+
export function isValid(appointmentTime, currentTime) {
68+
if (new Date(appointmentTime) < new Date(currentTime)) {
69+
return 'The appointment is not valid anymore.';
70+
} else {
71+
return 'The appointment is valid.';
72+
}
73+
}

exercises/concept/appointment-time/appointment-time.spec.js

+74
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,77 @@ import {
55
updateAppointment,
66
availableTimes,
77
} from './appointment-time';
8+
9+
describe('createAppointment', () => {
10+
test('creates appointment of 4 days later', () => {
11+
expect(createAppointment(4)).toBe(
12+
new Date(Date.now() + 4 * 24 * 3600 * 1000)
13+
);
14+
});
15+
16+
test('creates appointment of 124 days later', () => {
17+
expect(createAppointment(124)).toBe(
18+
new Date(Date.now() + 124 * 24 * 3600 * 1000)
19+
);
20+
});
21+
});
22+
23+
describe('getAppointment', () => {
24+
test('get appointment detail', () => {
25+
expect(getAppointment('24 April 2022 10:15 AM')).toEqual({
26+
year: 2022,
27+
month: 3,
28+
date: 24,
29+
hour: 10,
30+
minute: 15,
31+
});
32+
});
33+
});
34+
35+
describe('updateAppointment', () => {
36+
test('should update with one option', () => {
37+
expect(
38+
updateAppointment('09 February 2022 10:20 am', { month: 6 })
39+
).toEqual({ year: 2022, month: 6, date: 9, hour: 10, minute: 20 });
40+
});
41+
42+
test('should update with multiple options', () => {
43+
expect(
44+
updateAppointment('21 November 2022 10:20 pm', {
45+
year: 2023,
46+
month: 1,
47+
date: 12,
48+
hour: 1,
49+
minute: 29,
50+
})
51+
).toEqual({ year: 2023, month: 1, date: 12, hour: 1, minute: 29 });
52+
});
53+
54+
test('should update with option with zero as value', () => {
55+
expect(
56+
updateAppointment('17 December 2022 8:10 am', { minute: 0 })
57+
).toEqual({ year: 2022, month: 11, date: 17, hour: 8, minute: 0 });
58+
});
59+
});
60+
61+
describe('availableTimes', () => {
62+
test('get available times between two appointments', () => {
63+
expect(
64+
availableTimes('12 December 2022 10:20 am', '18 December 2022 9:30 am')
65+
).toBe(515400000);
66+
});
67+
});
68+
69+
describe('isValid', () => {
70+
test('appointmentTime is greater than currentTime', () => {
71+
expect(isValid('12 February 2022', '9 February 2022')).toEqual(
72+
'The appointment is valid.'
73+
);
74+
});
75+
76+
test('appointmentTime is less than currentTime', () => {
77+
expect(isValid('20 May 2022', '21 May 2022')).toEqual(
78+
'The appointment is not valid anymore.'
79+
);
80+
});
81+
});

0 commit comments

Comments
 (0)