← Blog## JavaScript Katas: Coin Combination

## Intro 🌐

## Understanding the Exercise❗

### Today’s exercise

## Thinking about the Solution 💭

## Implementation ⛑

### Result

## Playground ⚽

## Next Part ➡️

## Further Reading 📖

## Questions ❔

August, 2020

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.

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

My personal method:

- Input: What do I put in?
- Output: What do I want to get out?

Today, another `7 kyu`

kata,
meaning we slightly increase the difficulty.

Source: Codewars

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:

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

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:

- Find out how many times I need the 25 cents coin
- Find out how many times I need the 10 cents coin
- Find out how many times I need the 5 cents coin
- Find out how many times I need the 1 cents coin
- 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]`

✅

```
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;
}
```

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

You can play around with the code here

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.

If you want to read my latest stuff, get in touch with me!

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

Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.