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);