Created
February 6, 2022 15:15
-
-
Save jonybekov/2f38f1c8ade114ebfaa167b9bf71645e to your computer and use it in GitHub Desktop.
YDKJS 2: Getting started: Appendix B: Practicing Comparisons Problem Solution
This file contains hidden or 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 dayStart = "07:30"; | |
const dayEnd = "17:45"; | |
function scheduleMeeting(startTime,durationMinutes) { | |
let [startHour, startMin] = startTime.split(':'); | |
let durationHour = Math.floor(durationMinutes / 60); | |
let durationLeftMin = durationMinutes - durationHour * 60; | |
let [endStart, endMin] = [0,0]; | |
endHour = +startHour + durationHour; | |
endMin = +startMin + durationLeftMin; | |
if(endMin >= 60) { | |
endHour += 1; | |
endMin -= 60; | |
} | |
const withZero = (num) => Number(num) < 10 ? `0${num}` : `${num}`; | |
const newStartTime = `${withZero(startHour)}:${withZero(startMin)}` | |
const endTime = `${withZero(endHour)}:${withZero(endMin)}`; | |
return newStartTime >= dayStart && endTime <= dayEnd; | |
} | |
scheduleMeeting("7:00",15); // false | |
scheduleMeeting("07:15",30); // false | |
scheduleMeeting("7:30",30); // true | |
scheduleMeeting("11:30",60); // true | |
scheduleMeeting("17:00",45); // true | |
scheduleMeeting("17:30",30); // false | |
scheduleMeeting("18:00",15); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment