JavaScript Katas: Seconds To Minutes And Hours

miku86

miku86

Posted on August 28, 2020

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?
💖 💪 🙅 🚩
miku86
miku86

Posted on August 28, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Prototypes in JavaScript.
javascript Prototypes in JavaScript.

October 25, 2023

🚀A beginner's guide to Express.js
javascript 🚀A beginner's guide to Express.js

August 15, 2023

Learning Recursion
javascript Learning Recursion

March 5, 2023