From 7b0abe54e6e4f154f4aed3c5cdfc7ecc49f04072 Mon Sep 17 00:00:00 2001 From: vs-qaaug Date: Mon, 9 Sep 2024 13:45:12 +0300 Subject: [PATCH] Solution --- getCoinCombination.js | 21 +++++++++++++++++ getCoinCombination.test.js | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 getCoinCombination.js create mode 100644 getCoinCombination.test.js diff --git a/getCoinCombination.js b/getCoinCombination.js new file mode 100644 index 0000000..8af8895 --- /dev/null +++ b/getCoinCombination.js @@ -0,0 +1,21 @@ +'use strict'; + +/** + * @param {number} cents + * + * @returns {number[]} + */ +function getCoinCombination(cents) { + let currentAmount = cents; + const values = [1, 5, 10, 25]; + const coins = [0, 0, 0, 0]; + + for (let i = 3; i >= 0; i--) { + coins[i] = Math.floor(currentAmount / values[i]); + currentAmount -= coins[i] * values[i]; + } + + return coins; +} + +module.exports = { getCoinCombination }; diff --git a/getCoinCombination.test.js b/getCoinCombination.test.js new file mode 100644 index 0000000..75b38b0 --- /dev/null +++ b/getCoinCombination.test.js @@ -0,0 +1,47 @@ +'use strict'; + +describe('getCoinCombination', () => { + const { getCoinCombination } = require('./getCoinCombination'); + + test('should return [1, 0, 0, 0] for 1 cent', () => { + const result = getCoinCombination(1); + + expect(result).toEqual([1, 0, 0, 0]); + }); + + test('should return [1, 1, 0, 0] for 6 cents', () => { + const result = getCoinCombination(6); + + expect(result).toEqual([1, 1, 0, 0]); + }); + + test('should return [2, 1, 1, 0] for 17 cents', () => { + const result = getCoinCombination(17); + + expect(result).toEqual([2, 1, 1, 0]); + }); + + test('should return [0, 0, 0, 2] for 50 cents', () => { + const result = getCoinCombination(50); + + expect(result).toEqual([0, 0, 0, 2]); + }); + + test('should return [4, 0, 0, 3] for 79 cents', () => { + const result = getCoinCombination(79); + + expect(result).toEqual([4, 0, 0, 3]); + }); + + test('should return [0, 0, 0, 0] for 0 cents', () => { + const result = getCoinCombination(0); + + expect(result).toEqual([0, 0, 0, 0]); + }); + + test('should return [0, 0, 1, 0] for 10 cents', () => { + const result = getCoinCombination(10); + + expect(result).toEqual([0, 0, 1, 0]); + }); +});