DEV Community

Cover image for JavaScript Katas: Coin Combination
miku86
miku86

Posted on

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Result

console.log(coinCombo(51));
// [1, 0, 0, 2] βœ…

console.log(coinCombo(26));
// [1, 0, 0, 1] βœ…
Enter fullscreen mode Exit fullscreen mode

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?

Top comments (3)

Collapse
 
kosich profile image
Kostia Palchyk

This took me a while:

const coinCombo = [25, 10, 5, 1]
  .reduceRight((a, c) => n => [Math.floor(n/c), ...a(n % c)], () => [])

I believe that understanding the solution is part of the fun, so I usually don't add explanation here not to ruin that deduction exercise. Though, I'd be happy to explain/discuss it if someone asks. Is that ok? Michael, what do you think?

Collapse
 
miku86 profile image
miku86

Hey Kostia,

nice solution. Think I never used reduceRight in real life.
Sure, you can do that!

Collapse
 
gerges27 profile image
Gerges Nady

look at this solution

let coinCombo = (number) => {
let count25 = number / 25
let reminderOf25 = number % 25
let count10 = reminderOf25 / 10
let reminderOf10 = reminderOf25 % 10
let count5 = reminderOf10 / 5
let reminderOf5 = reminderOf10 % 5
let count1 = reminderOf5 / 1
console.log([parseInt(count1), parseInt(count5), parseInt(count10), parseInt(count25)])
}