DEV Community

Cover image for JavaScript Katas: Seconds To Minutes And Hours

JavaScript Katas: Seconds To Minutes And Hours

miku86 on August 28, 2020

Intro 🌐 Problem solving is an important skill, for your career and your life in general. That's why I take interesting katas of all lev...
Collapse
 
aissshah profile image
aishah

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.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Why write 4 lines when you can write 1? 😁

Collapse
 
miku86 profile image
miku86

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.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
const toMinutesAndHours = s=>`${~~(p=s/3600)} hour(s) and ${~~(p%1*60)} minute(s)`;
Enter fullscreen mode Exit fullscreen mode

By the way, you missed the seconds parameter in your function definitions. Running them gives an error.__

Collapse
 
lbermudez profile image
lbermudez

But where is var p declared? Only one line is not posible ;)
It would be:

const toMinutesAndHours = (num) => {
    let p;
    return `${~~(p = num / 3600)} hour(s) and ${~~((p % 1) * 60)} minute(s)`;
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Perfectly possible. There was no rule about not polluting the global namespace. The function works just fine

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Or, if you don't want to pollute globals:

const toMinutesAndHours = (s,p=s/3600)=>`${~~p} hour(s) and ${~~(p%1*60)} minute(s)`
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
lbermudez profile image
lbermudez • Edited

Ok, I executed it on node.js.
The latest solution change the contract of the function.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

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 😀

Thread Thread
 
lbermudez profile image
lbermudez

yeah! It's cool, no problem! 😉

Collapse
 
kosich profile image
Kostia Palchyk

Happy 30th kata, Michael! 🍰
Great work 👍

Collapse
 
miku86 profile image
miku86

Hehe thanks!

I think I will rework the katas,
remove some clutter and put them into a PDF.
Ideas on this?

Collapse
 
kosich profile image
Kostia Palchyk • Edited

🙂

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!)

Thread Thread
 
miku86 profile image
miku86 • Edited

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.

Thread Thread
 
kosich profile image
Kostia Palchyk

Yeah, I think it has been a couple years since I used PDFs as knowledge source, website might be more convenient today.

Any suggestions for an existing tool that can create a nice website from markdown files?

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

Thread Thread
 
miku86 profile image
miku86

Imho, it partially depends on your current website stack (thought you might want to amend that to your existing one).

I'm using Gatsby and I'm using it with .mdx.

Also, recently I've finally tried gitbook — had a great experience with it!

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.

Thread Thread
 
kosich profile image
Kostia Palchyk

Sometimes new ideas postpone a solid release 🙂

Collapse
 
gerges27 profile image
Gerges Nady

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))
}