JavaScript Katas: Seconds To Minutes And Hours
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-28 16:21:43

---
title: "JavaScript Katas: Seconds To Minutes And Hours"
description: "We learn how to use while, Math.floor"
published: true
tags: ["JavaScript", "Beginners", "Webdev", "Codenewbie"]
series: JavaScript Katas with miku86
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/0jfyzv0grswtx1mlo17m.jpg
---

## 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](https://www.codewars.com/kata/5865cff66b5699883f0001aa)

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
1. Find out how many full hours you get from the minutes and remove them from minutes
1. 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) ⛑

```js
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

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

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

---

## Implementation (floor) ⛑

```js
function toMinutesAndHours() {
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);

  return `${hours} hour(s) and ${minutes} minute(s)`;
}
```

### Result

```js
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](https://repl.it/@miku86/js-katas-seconds-to-minutes-and-hours)

---

## 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](https://forms.gle/RmQLq7CLDiD2Ptmw5).

If you want to read my latest stuff, [get in touch with me!](https://miku86.com/newsletter)

---

## Further Reading 📖

- [while](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
- [Math.floor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)

---

## Questions ❔

- How often do you do katas?
- Which implementation do you like more? Why?
- Any alternative solution?