Currency MoneyBack

Just for fun

by maxell4o

JavaScript

function coinsToReturn(amount, type = 'BGN') {
  let _type = {
  	BGN: [100, 50, 20, 10, 5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
  };
  return result = _type[type].reduce((acc, value) => {
    let count = Math.floor(amount / value);
    amount = (amount % value).toFixed(2);
    return acc + (count && value + `(${count}) ` || ``);
  }, ``);
}

console.log(coinsToReturn(25.4)); // 25лв 43ст