Intro 🌐
Problem solving is an important skill, for your career and your life in general.
That's why I take interesting katas of all levels, customize them and explain how to solve them.
Today's exercise
Today, another 7 kyu
kata,
meaning we slightly increase the difficulty.
Source: Codewars
Write a function toMinutesAndHours
, that accepts one parameter: seconds
.
Given a number, e.g. 3601
,
return a string describing how many hours and minutes comprise that many seconds, any remaining seconds left over are ignored,
e.g. 1 hour(s) and 0 minute(s)
.
Input: a number.
Output: a string.
Thinking about the Solution 💭
First, we need to understand the exercise! If we don't understand it, we can't solve it!.
I think I understand the exercise (= what I put into the function and what I want to get out of it).
Now, I need the specific steps to get from input to output.
I try to do this in small baby steps:
- Find out how many full minutes you get from the seconds
- Find out how many full hours you get from the minutes and remove them from minutes
- Return the string with hours and minutes
Example:
- Input:
3601
- Find out how many full minutes you get from the seconds:
60
- Find out how many full hours you get from the minutes and remove them from minutes:
1
hours and0
minutes - Output:
"1 hour(s) and 0 minute(s)"
✅
Implementation (while) ⛑
function toMinutesAndHours() {
const S_IN_M = 60;
const M_IN_H = 60;
let minutes = 0;
while (seconds - S_IN_M >= 0) {
minutes++;
seconds -= S_IN_M;
}
let hours = 0;
while (minutes - M_IN_H >= 0) {
hours++;
minutes -= M_IN_H;
}
return `${hours} hour(s) and ${minutes} minute(s)`;
}
Result
console.log(toMinutesAndHours(3600));
// "1 hour(s) and 0 minute(s)" ✅
console.log(toMinutesAndHours(3601));
// "1 hour(s) and 0 minute(s)" ✅
Implementation (floor) ⛑
function toMinutesAndHours() {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
return `${hours} hour(s) and ${minutes} minute(s)`;
}
Result
console.log(toMinutesAndHours(3600));
// "1 hour(s) and 0 minute(s)" ✅
console.log(toMinutesAndHours(3601));
// "1 hour(s) and 0 minute(s)" ✅
Playground ⚽
You can play around with the code here
Next Part ➡️
Great work!
We learned how to use while
, Math.floor
.
I hope you can use your new learnings to solve problems more easily!
Next time, we'll solve another interesting kata. Stay tuned!
If I should solve a specific kata, shoot me a message here.
If you want to read my latest stuff, get in touch with me!
Further Reading 📖
Questions ❔
- How often do you do katas?
- Which implementation do you like more? Why?
- Any alternative solution?
Top comments (18)
Thanks for this post. It was interesting to see how you approached the problem, broke it down and came up with different solutions.
I prefer the floor method as it looks cleaner to me and more concise. Why write 15 lines when you can write 4? Though let me know if I am wrong.
Why write 4 lines when you can write 1? 😁
Hey there,
I think it's a good to stay sharp,
so I try to come up with different solutions,
depending on how much I enjoy the problem.
By the way, you missed the
seconds
parameter in your function definitions. Running them gives an error.__But where is var p declared? Only one line is not posible ;)
It would be:
Perfectly possible. There was no rule about not polluting the global namespace. The function works just fine
Or, if you don't want to pollute globals:
Ok, I executed it on node.js.
The latest solution change the contract of the function.
So? Still solves problem, and still works. Just showing how it could be written in a short way. Code golf. I'm not advocating for this style in project code.
It still only accepts one parameter (function length is still 1) - nothing in the description of the task says we can't also have an optional parameter 😀
yeah! It's cool, no problem! 😉
Happy 30th kata, Michael! 🍰
Great work 👍
Hehe thanks!
I think I will rework the katas,
remove some clutter and put them into a PDF.
Ideas on this?
🙂
Sounds great!
Do you have a particular use case in mind for PDFs? (nowadays I often prefer websites over pdfs)
Maybe consider adding
smart
/best practice
solutions from Codewars (lots of brilliant solutions there!)Great idea!
PDF was my first idea, because it is easy to download, to use at home and to archive.
But the more I play around with it, the more annoying it becomes to convert 30 markdown files to PDF, while giving it a good user experience 👀
Any suggestions for an existing tool that can create a nice website from markdown files?
Otherwise I will look for a simple Gatsby theme.
Yeah, I think it has been a couple years since I used PDFs as knowledge source, website might be more convenient today.
Imho, it partially depends on your current website stack (thought you might want to amend that to your existing one).
I've used react-static and then switched to nextjs for
.md
sourced pages, both are good enough solutions. Haven't had a chance to try Gatsby yet, should be a solid solution from what I've read. Btw, if you haven't already — it's a good chance to try.mdx
(still on my to-try list).Also, recently I've finally tried gitbook — had a great experience with it!
(though it wont fit if you'd want to extend it w/ playground or something)
GL
I'm using Gatsby and I'm using it with
.mdx
.Actually a good idea to have a look into gitbook.
And to have a look into a playground thingy, to remove the dependency to repl.it.
Thanks for your ideas.
Sometimes new ideas postpone a solid release 🙂
let toMinutesAndHours = (seconds) => {
let countHour = seconds / (60 * 60)
let reminderOfHour = seconds % 3600
let countMinute = reminderOfHour / 60
console.log(
${parseInt(countHour)} Hour(s) ${parseInt(countMinute)} Minute(s)
)}