I found some interesting exercises in the new, second edition of Kyle Simpson’s You Don’t Know JS series. At the end of the first book, he provided the reader with some practical examples of the topics learned. This time we will be solving a neat comparisons example.
By the way, the You Don’t Know JS series is probably the single best resource on JavaScript and its core mechanisms on the Internet. To make things even sweeter the first edition is completely free – so check it out if you want to learn more about the nitty-gritty details in JS.
Comparisons Exercise
scheduleMeeting(..)
should take a start time (in 24-hour format as a string “hh:mm”) and a meeting duration (number of minutes). It should return true
if the meeting falls entirely within the workday (according to the times specified in dayStart
and dayEnd
); return false
if the meeting violates the workday bounds.
const dayStart = "07:30"; const dayEnd = "17:45"; function scheduleMeeting(startTime,durationMinutes) { // ..TODO.. } 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
Try to solve this yourself first. Consider the usage of equality and relational comparison operators, and how coercion impacts this code. Once you have code that works, compare your solution(s) to the code in “Suggested Solutions” at the end of this appendix. (Kyle Simpson’s solutions can be found in the book!)
Analysis of the Problem
The first thing that I noticed is that we accept two-hour variants, formatted as hh, which means that we either accept 07 or 7 as the valid value. We will most probably use a regex or string manipulation to extract data from the start time. We need to parse the time given into a total time consisting only of minutes. Actually, let us do that right away. We’ll assume that the inputs are of the proper type, logical and that no additional validation is needed for this example.
const [hours, minutes] = startTime.split(":"); const startTimeConverted = hours * 60 + Number(minutes);
Another thing we’ve done here is that we converted the starting time into a new, more numeric value – that will help us in our goal of comparisons (it is much easier to compare numbers than strings for example). We would need this logic for the dayStart
and dayEnd
values too, so we will export this logic into a separate function instead of duplicating the lines.
const startTimeConverted = convertToTotalTime(startTime); const dayStartConverted = convertToTotalTime(dayStart); const dayEndConverted = convertToTotalTime(dayEnd); function convertToTotalTime(time) { const [hours, minutes] = time.split(":"); return hours * 60 + Number(minutes); }
Now we just need to write some logic to actually compare the values which is the end goal of our exercise.
return startTimeConverted < dayStartConverted || (startTimeConverted + durationMinutes) > dayEndConverted ? false : true;
So what is going on above? We’ve just checked whether the starting time is earlier than the start of the workday and whether the ending date plus the meeting duration date lasts longer than the end of the workday! This solution will work for all of the cases above.
Our final solution can be found in the snippet below.
function scheduleMeeting(startTime,durationMinutes) { const startTimeConverted = convertToTotalTime(startTime); const dayStartConverted = convertToTotalTime(dayStart); const dayEndConverted = convertToTotalTime(dayEnd); return startTimeConverted < dayStartConverted || (startTimeConverted + durationMinutes) > dayEndConverted ? false : true; function convertToTotalTime(time) { const [hours, minutes] = time.split(":"); return hours * 60 + Number(minutes); } }
Final Words
It was quite an interesting exercise, and I’ve managed to do it in only a couple of lines of code. If you want to see Kyle’s solution please check the book!
For more articles please click below, or check the blog.
- Largest Palindrome Product
- Largest Prime Factor
- Even Fibonacci Numbers
- Multiples of 3 or 5
- How to find the missing number in a given integer array of 1 to 100?
- Understanding Javascript prototypes
- Difference between Promise.all() and Promise.race()
- JavaScript generators
- Using this and arguments
- Arrow functions in JavaScript
Pingback: JavaScript - Practicing Closure | The Dukh Chronicles
Pingback: JavaScript - Practicing Prototypes | The Dukh Chronicles
Comments are closed.