JavaScript Katas: Coin Combination
Tags: javascript, beginners, webdev, codenewbie
Published: 2020-08-12 15:12:46

---
title: "JavaScript Katas: Coin Combination"
description: "We learn how to use Array, for of, while, indexOf, %"
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.

---

## Understanding the Exercise❗

First, we need to understand the exercise!
If you don't understand it, you can't solve it!.

My personal method:

1. Input: What do I put in?
2. Output: What do I want to get out?

---

### Today's exercise

Today, another `7 kyu` kata,
meaning we slightly increase the difficulty.

Source: [Codewars](https://www.codewars.com/kata/564d0490e96393fc5c000029)

Write a function `coinCombo`, that accepts one parameter: `cents`.

Given a number of cents, e.g. `51`,
return the minimum number of coins combination of the same value, e.g. `[1, 0, 0, 2]`.

The function should return an array where:

```js
coins[0] = 1 cent
coins[1] = 5 cents
coins[2] = 10 cents
coins[3] = 25 cents
```

Example: `coinCombo(51)` needs two `25 cents` and one `1 cent` => `[1, 0, 0, 2]`

---

Input: a number.

Output: an array of numbers.

---

## Thinking about the Solution 💭

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 times I need the 25 cents coin
1. Find out how many times I need the 10 cents coin
1. Find out how many times I need the 5 cents coin
1. Find out how many times I need the 1 cents coin
1. Return the array with the combination of the coins

---

Example:

- Input: `51`
- Find out how many times I need the 25 cents coin: `2`, because `2 * 25 = 50` => `1` left
- Find out how many times I need the 10 cents coin: `0`, because I got only `1` left => `1` left
- Find out how many times I need the 5 cents coin: `0`, because I got only `1` left => `1` left
- Find out how many times I need the 1 cents coin: `1`, because `1 * 1 = 1` => `0` left
- Return the array with the combination of the coins: `[1, 0, 0, 2]`
- Output: `[1, 0, 0, 2]` ✅

---

## Implementation ⛑

```js
function coinCombo() {
  // all coin values
  const coinValues = [25, 10, 5, 1];

  // array for the output, filled with zeros
  const coins = Array(coinValues.length).fill(0);

  let currentCents = cents;

  // iterate over the coins
  for (const coin of coinValues) {
    // only do this if there are some coins left
    while (currentCents >= coin) {
      // find out how many cents are left
      // and how many times the current coins fit into the current cents
      const remainder = currentCents % coin;
      const increaseBy = (currentCents - remainder) / coin;
      currentCents = currentCents % coin;
      const index = coinValues.length - 1 - coinValues.indexOf(coin);
      coins[index] += increaseBy;
    }
  }

  return coins;
}
```

### Result

```js
console.log(coinCombo(51));
// [1, 0, 0, 2] ✅

console.log(coinCombo(26));
// [1, 0, 0, 1] ✅
```

---

## Playground ⚽

You can play around with the code [here](https://repl.it/@miku86/js-katas-coin-combination)

---

## Next Part ➡️

Great work!

We learned how to use `Array`, `for of`, `while`, `indexOf`, `%`.

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 📖

- [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array)
- [for of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
- [while](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
- [indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
- [%](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)

---

## Questions ❔

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