DEV Community

Discussion on: Advent.js🎅🏼| #9: Agrupando cosas automáticamente

Collapse
 
nahukas profile image
Nahuel Castro
function getCoins(change) {

  let response = [];
  const coins = [50, 20, 10, 5, 2, 1];
  coins.forEach(coin => {
    if (change >= coin) {
        response.push(Math.trunc(change/coin));
      change = change % coin;
    } else {
        response.push(0);
    }
  });
  return response.reverse();
}
Enter fullscreen mode Exit fullscreen mode