Created
August 11, 2020 11:07
-
-
Save anoriqq/6c63869c3fe63e6c8f1096dc9515d20f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const assert = require("assert").strict; | |
| const ITEMS = { | |
| お茶: 120, | |
| コーラ: 150, | |
| エナジードリンク: 280, | |
| }; | |
| const CURRENCY = { | |
| '1000': 0, | |
| '500': 0, | |
| '100': 0, | |
| '50': 0, | |
| '10': 0, | |
| }; | |
| function calcChange(item, paidMoney) { | |
| if (!Object.keys(ITEMS).includes(item)) throw 'Error'; | |
| let result = paidMoney - ITEMS[item]; | |
| const x = Object.fromEntries( | |
| Object.keys(CURRENCY) | |
| .sort((a, b) => b - a) | |
| .map((x) => { | |
| const value = result; | |
| result = Math.floor(value % Number(x)); | |
| return [x, Math.floor(value / Number(x))]; | |
| }), | |
| ); | |
| return x; | |
| } | |
| const expected = {10: 3, 50: 1, 100: 3, 500: 1, 1000: 0}; | |
| assert.deepEqual(calcChange('お茶', 1000), expected); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment