DEV Community

Discussion on: Daily Challenge #45 - Change Machine

Collapse
 
aminnairi profile image
Amin • Edited

JavaScript

Pure variant of what has already been made in JavaScript.

function change(money) {
  return [25, 10, 5, 1].reduce(function([computedChange, remainingMoney], coin) {
    return [Object.assign(computedChange, {
      [coin]: Math.floor(remainingMoney / coin)
    }), remainingMoney % coin];
  }, [{}, money])[0];
}

Performance

I am curious about the performance in those kind of challenge. I am totally aware that JavaScript is not the fastest to do this kind of thing and the idea is not to bash any language of implementation. I'm just a guy curious about that. If some of you are willing to take the habit to post the performance for one milion iterations that would be cool to compare with others!

// one milion iterations
for (let index = 0; index < 1000000; index++) {
  change(31);
}

console.timeEnd("time");
// time: 21670.730ms

Source-Code

Available online.