Skip to content

Instantly share code, notes, and snippets.

@dobeerman
Created August 10, 2021 05:15
Show Gist options
  • Save dobeerman/3b4692fbe60abd89db39e8ba5f0c513b to your computer and use it in GitHub Desktop.
Save dobeerman/3b4692fbe60abd89db39e8ba5f0c513b to your computer and use it in GitHub Desktop.
The code demonstrates an elegant solution to a money dispensing machine problem.
/**
* Say, a customer requires $247 from an ATM machine.
* What is the combination of bank notes ($100, $50, $20, $10, $5, $1)
* that satisfies that request?
*/
class RequestBill {
private pairs: number[][];
constructor(private amount: number) {
this.pairs = [];
}
get(bill: number) {
var count = Math.floor(this.amount / bill);
this.amount -= count * bill;
this.pairs.push([count, bill])
return this;
}
getPairs() {
return this.pairs
}
}
const request = new RequestBill(378);
request.get(100).get(50).get(20).get(10).get(5).get(1);
console.log(request.getPairs())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment