Skip to content

Instantly share code, notes, and snippets.

@oceangravity
Created June 19, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oceangravity/3adee8b9e0f84f932b4ee97d99431330 to your computer and use it in GitHub Desktop.
Save oceangravity/3adee8b9e0f84f932b4ee97d99431330 to your computer and use it in GitHub Desktop.
class CoinChange {
constructor(changes) {
this.coins = 0;
this.changes = changes;
this.output = {};
};
getEquivalence(equivalence) {
let output = 0;
if(this.coins / equivalence > 1){
output = (this.coins - (this.coins % equivalence)) / equivalence;
this.coins = this.coins % equivalence;
}
return output;
};
getChanges(coins) {
const me = this;
me.coins = coins;
Object.keys(me.changes).forEach((k, v, er) => {
me.output[k] = me.getEquivalence(me.changes[k]);
});
return me.output;
};
}
class CoinChangeByCountry {
constructor() {
this.config = {};
};
addCountry(country, config) {
this.config[country] = new CoinChange(config);
};
getChanges(country, coins) {
return this.config[country].getChanges(coins);
};
}
const countries = new CoinChangeByCountry();
countries.addCountry('US', {
Dollar: 100,
Quarter: 25,
Dime: 10,
Nickel: 5,
Cents: 1
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment