DEV Community

Discussion on: Daily Challenge #45 - Change Machine

Collapse
 
chrisachard profile image
Chris Achard

JavaScript with reduce:

const COINS = [25, 10, 5, 1]

const change = (cents) => {

  let remainingValue = cents

  const coinCount = COINS.reduce((count, coin) => {
    count[coin] = Math.floor(remainingValue / coin)
    remainingValue = remainingValue % coin
    return count
  }, {})

  return coinCount
}