Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new concept exercise - appointment-time #1926

Closed
Prev Previous commit
Next Next commit
appointment-time.spec.js and appoint-time exemplar.js file ready
SalahuddinAhammed committed Oct 5, 2022

Verified

This commit was signed with the committer’s verified signature.
bukka Jakub Zelenka
commit 2b932d4537581cd8bcc11f500e1918fbf364f4c0
72 changes: 72 additions & 0 deletions exercises/concept/appointment-time/.meta/exemplar.js
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
// @ts-check

/**
* Create an appointment
*
* @param {number} days
*/
export function createAppointment(days) {
return new Date(Date.now() + days * 24 * 3600 * 1000);
}

/**
* Get details of an appointment
*
* @param {string} appointmentTime
*/
export function getAppointment(appointmentTime) {
const appointmentDate = new Date(appointmentTime);
return {
year: appointmentDate.getFullYear(),
month: appointmentDate.getMonth(),
date: appointmentDate.getDate(),
hour: appointmentDate.getHours(),
minute: appointmentDate.getMinutes(),
};
}

/**
* Update an appointment with given options
*
* @param {string} appointmentTime
* @param {object} options
*/
export function updateAppointment(appointmentTime, options) {
let appointmentDate = new Date(appointmentTime);
if (options.year !== undefined) appointmentDate.setFullYear(options.year);
if (options.month !== undefined) appointmentDate.setMonth(options.month);
if (options.date !== undefined) appointmentDate.setDate(options.date);
if (options.hour !== undefined) appointmentDate.setHours(options.hour);
if (options.minute !== undefined) appointmentDate.setMinutes(options.minute);

return {
year: appointmentDate.getFullYear(),
month: appointmentDate.getMonth(),
date: appointmentDate.getDate(),
hour: appointmentDate.getHours(),
minute: appointmentDate.getMinutes(),
};
}

/**
* Get available times between two appointment
*
* @param {string} appointmentATime
* @param {string} appointmentBTime
*/
export function availableTimes(appointmentATime, appointmentBTime) {
return new Date(appointmentBTime) - new Date(appointmentATime);
}

/**
* Get available times between two appointment
*
* @param {string} appointmentTime
* @param {string} currentTime
*/
export function isValid(appointmentTime, currentTime) {
if (new Date(appointmentTime) < new Date(currentTime)) {
return 'The appointment is not valid anymore.';
} else {
return 'The appointment is valid.';
}
}
74 changes: 74 additions & 0 deletions exercises/concept/appointment-time/appointment-time.spec.js
Original file line number Diff line number Diff line change
@@ -5,3 +5,77 @@ import {
updateAppointment,
availableTimes,
} from './appointment-time';

describe('createAppointment', () => {
test('creates appointment of 4 days later', () => {
expect(createAppointment(4)).toBe(
new Date(Date.now() + 4 * 24 * 3600 * 1000)
);
});

test('creates appointment of 124 days later', () => {
expect(createAppointment(124)).toBe(
new Date(Date.now() + 124 * 24 * 3600 * 1000)
);
});
});

describe('getAppointment', () => {
test('get appointment detail', () => {
expect(getAppointment('24 April 2022 10:15 AM')).toEqual({
year: 2022,
month: 3,
date: 24,
hour: 10,
minute: 15,
});
});
});

describe('updateAppointment', () => {
test('should update with one option', () => {
expect(
updateAppointment('09 February 2022 10:20 am', { month: 6 })
).toEqual({ year: 2022, month: 6, date: 9, hour: 10, minute: 20 });
});

test('should update with multiple options', () => {
expect(
updateAppointment('21 November 2022 10:20 pm', {
year: 2023,
month: 1,
date: 12,
hour: 1,
minute: 29,
})
).toEqual({ year: 2023, month: 1, date: 12, hour: 1, minute: 29 });
});

test('should update with option with zero as value', () => {
expect(
updateAppointment('17 December 2022 8:10 am', { minute: 0 })
).toEqual({ year: 2022, month: 11, date: 17, hour: 8, minute: 0 });
});
});

describe('availableTimes', () => {
test('get available times between two appointments', () => {
expect(
availableTimes('12 December 2022 10:20 am', '18 December 2022 9:30 am')
).toBe(515400000);
});
});

describe('isValid', () => {
test('appointmentTime is greater than currentTime', () => {
expect(isValid('12 February 2022', '9 February 2022')).toEqual(
'The appointment is valid.'
);
});

test('appointmentTime is less than currentTime', () => {
expect(isValid('20 May 2022', '21 May 2022')).toEqual(
'The appointment is not valid anymore.'
);
});
});