Solving challenges from HackerRank with JavaScript part 2.
Here in part 1 we’ve solved Time Conversion challenge, which was pretty ease.
Our task was to convert the 12-hour time format to 24-hour. We’ve got some random input data like 07:05:45 and our goal is to return 19:05:45 as output.
The entire code was only 5-lines.
Furthermore in the comments Andre Glauser gave a one-line solution to the problem!
I would sort this code out a little bit for those who don’t understand it.
First — replace() method — simply replaces the first parameter with the second parameter in the given string. In our case, it looks for AM or PM characters and replaces them with empty space.
Second — is already familiar method from part one — split().
Next — array’s method map(). It will iterate existing array and return a new one without changing the specified array. It takes a callback function with three parameters, but here we use only two — currentValue and index, and object as second parameter.
Again, we will not look at logic, because it has already been done in part one. But one more thing we’re interested in — RegExp’s method test(). It searches for a match between a RegExp and a given string, returns a Boolean value depending on whether there was a match or not.
That’s it for Time Conversion challenge!
Mini-Max Sum Challenge
Our next challenge — not difficult, but quite interesting!
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
Here we have an array of five numbers, and our goal is to return a string with only two numbers-the maximum sum of four, and the minimum sum of four.
Let’s talk about logic!
For the first time you can think about counting all the numbers with each other — and this is really a bad way to solve the problem. Not only because it’s too slow, but because you have to write a lot of code. But what if I tell you that we can solve it with just one line of code! (In fact, in terms of performance and readability, we’ll split it into two lines.)
Actually our logic is quite simple — we’ll count the sum of all the numbers, and then we’ll just find the difference between the sum and the maximum number of a given array and the sum and the minimum number of that array. That’s it!
Let’s code!
Reduce method
Here’s our code that will give us the sum. We use the reduce() method of the array. It takes two arguments — a callback function and an initial value, which can be a string, a number, an object, or whatever you like. And then returns a new value without changing the specified array.
Math.min() and Math.max()
Next, as I promised, we will write only one line of code, which will give us the desired line with two numbers.
Here’s our code! If you are not familiar with the ES6 spread operator, here is a simple example of using it:
Also take a look at template literals. And our line we can rewrite in ES5 as:
So whole code now looks like:
And that’s it! We solved another problem. If you have any other solutions, please add a comment even if it is not very good in terms of readability and/or performance.