JavaScript Katas: Coin Combination

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

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.


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
  2. Find out how many times I need the 10 cents coin
  3. Find out how many times I need the 5 cents coin
  4. Find out how many times I need the 1 cents coin
  5. 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 β›‘

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

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


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.

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?