Last active
March 13, 2019 14:23
-
-
Save exwar/37433549efa91b3d7e58640d59ef6753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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