1234567891011121314151617181920212223242526
const availableChange = { hundred: 100, twenty: 20, ten: 10, five: 5, one: 1, quarter: .25, dime: .10, nickel: .05, penny: .01, } function breakMoneyIntoComponents(money) { const result = {}; let remaining = money; while(Math.floor(remaining * 100) > 0) { for(key in availableChange) { if (remaining >= availableChange[key]) { result[key] = (result[key] || 0) + 1; remaining -= availableChange[key]; break; } } } return result; }
breakMoneyIntoComponents(1.27)