DEV Community

Cover image for JavaScript Katas: Seconds To Minutes And Hours
miku86
miku86

Posted on

JavaScript Katas: Seconds To Minutes And Hours

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:

  1. Find out how many full minutes you get from the seconds
  2. Find out how many full hours you get from the minutes and remove them from minutes
  3. 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 and 0 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)`;
}
Enter fullscreen mode Exit fullscreen mode

Result

console.log(toMinutesAndHours(3600));
// "1 hour(s) and 0 minute(s)" ✅

console.log(toMinutesAndHours(3601));
// "1 hour(s) and 0 minute(s)" ✅
Enter fullscreen mode Exit fullscreen mode

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)`;
}
Enter fullscreen mode Exit fullscreen mode

Result

console.log(toMinutesAndHours(3600));
// "1 hour(s) and 0 minute(s)" ✅

console.log(toMinutesAndHours(3601));
// "1 hour(s) and 0 minute(s)" ✅
Enter fullscreen mode Exit fullscreen mode

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)

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