Skip to content

Instantly share code, notes, and snippets.

@oceangravity
Created June 19, 2018 11:53
Show Gist options
  • Save oceangravity/afded69ca0a5d72786bc882b15b3ce71 to your computer and use it in GitHub Desktop.
Save oceangravity/afded69ca0a5d72786bc882b15b3ce71 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