Skip to content

Instantly share code, notes, and snippets.

@exwar
Last active March 13, 2019 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exwar/37433549efa91b3d7e58640d59ef6753 to your computer and use it in GitHub Desktop.
Save exwar/37433549efa91b3d7e58640d59ef6753 to your computer and use it in GitHub Desktop.
const availableHours = 5.5;
const availableMeetings = [
{ title: 'Welcome greetings', duration: 5.45 },
{ title: 'Stand Up', duration: 0.05 },
{ title: 'Product catch up', duration: 1.5 },
{ title: 'Candidate interview', duration: 1.0 },
{ title: 'Coffee break', duration: 1.0 },
{ title: 'Spring planning', duration: 2.0 },
{ title: 'Sub-project estimation', duration: 2.0 },
];
const trimToFixedFloat = numberObject => {
const trimmedFloat = parseFloat(parseFloat(numberObject).toFixed(2));
return isNaN(trimmedFloat) ? new Error('Not a Number!') : trimmedFloat;
};
const getSchedule = (availableMeetings, availableHours) => {
const resultMeetings = [];
let hoursAreLeft = trimToFixedFloat(availableHours);
for (meeting of availableMeetings) {
const meetingDuration = trimToFixedFloat(meeting.duration);
if (hoursAreLeft === 0) {
console.log('You have no more free hours left!');
break;
}
if (hoursAreLeft >= meetingDuration) {
resultMeetings.push(meeting);
hoursAreLeft = trimToFixedFloat(hoursAreLeft - meetingDuration);
}
}
if (hoursAreLeft !== 0) console.log('Free hours left: ', hoursAreLeft);
return resultMeetings;
};
console.log(getSchedule(availableMeetings, availableHours));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment