Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              // unit to value mapping
const MoneyUnits = {
	'ONE HUNDRED': 10000,
	'TWENTY': 2000,
	'TEN': 1000,
	'FIVE': 500,
	'ONE': 100,
	'QUARTER': 25,
	'DIME': 10,
	'NICKEL': 5,
	'PENNY': 1
}

// CashRegister Money unit
function MoneyUnit(id, value, amount = 0) {
	this.id = id;
	this.value = value;
	this.amount = amount;
}

MoneyUnit.prototype = {
	addAmount(amount) {
		this.amount += amount;
		return this.value * amount;
	},
	removeAmount(amount) {

		if (this.amount < amount) return false;
		this.amount -= amount;

		return this.value * amount;
	},
	getValue(amount) {
		if (!arguments.length) {
			amount = this.amount;
		}
		return this.value * amount;
	},
	getChange(value) {
		if (this.amount == 0 || value < this.value) return 0;
		let amount = Math.floor(value / this.value);
		return amount <= this.amount ? amount : this.amount;
	},
}



function CashRegister(unitAmounts, shiftValue = 2) {
	this.shiftValue = shiftValue;
	this._initializeStore();
	this.addUnits(unitAmounts);
}

CashRegister.prototype = {
	
	_initializeStore() {
		this.store = Object.keys(MoneyUnits).reduce((memo, id) => {
			let value = MoneyUnits[id];
			memo[id] = new MoneyUnit(id, value);
			return memo;
		}, {});
	},
	
	// adds unit amounts to store.
	addUnits(unitAmounts) {
		Object.keys(unitAmounts).forEach(id => {
			let amount = unitAmounts[id];
			let unit = this.getUnit(id);
			unit.addAmount(amount);
		});
	},
	
	getUnits() {
		return Object.keys(this.store).map(id => this.getUnit(id));
	},
	
	getUnit(id) {
		return this.store[id];
	},
	
	_getValue(id) {
		if (arguments.length > 0) {
			return this.getUnit(id).getValue();
		}
		
		return this.getUnits().reduce((memo, unit) => memo + unit.getValue(), 0);
		
	},
	
	getValue() {
		let value = this._getValue().toString();
		let right = value.substring(value.length - this.shiftValue);
		let left = value.substring(0, value.length - this.shiftValue) || '0';
		return parseFloat(`${left}.${right}`, 10);
	},
	
	findChange(value) {
		value = Math.round(value * Math.pow(10, this.shiftValue));
		return this._findChange(value);
	},
	
	_findChange(value, units, change = {}) {
		!units && (units = this.getUnits().sort((u1, u2) => u2.value - u1.value));

		if (!units.length) {
			if (value > 0) {
				return;
			}
			return change;
		}
		
		let unit = units.shift();
		let unitAmounts = unit.getChange(value);

		if (!unitAmounts) {
			return this._findChange(value, units, change);
		}
		value = value - unit.getValue(unitAmounts);
		change[unit.id] = unitAmounts;
		
		return this._findChange(value, units, change);
		
	},
	tryWidthdraw(value) {
		let change = this.findChange(value);
		if (!change) {
			return false;
		}
		return this._withdraw(change);
	},
	
	_withdraw(change) {
		return Object.keys(change).reduce((memo, key) => {
			let unit = this.getUnit(key);
			return memo && !!unit.removeAmount(change[key]);
		}, true);
	},
	
	getState() {
		return this.getUnits().reduce((memo, unit) => {
			memo[unit.id] = unit.amount;
			return memo;
		}, {});
	}
	
}

console.clear();

// { unitId: unitAmount }
let cashRegister = new CashRegister({
	'ONE HUNDRED': 20,
	'TWENTY': 8,
	'TEN': 10,
	'FIVE': 20,
	'ONE': 30,
	'QUARTER': 40,
	'DIME': 60,
	'NICKEL': 70,
	'PENNY': 100
});

/*

// current amount of money in cashRegister
let value = cashRegister.getValue();
console.log('res: ', value);

// trying to find if given ammount can be withdrawn
let change = cashRegister.findChange(1123.17);
console.log('the change is ', change);

//trying to withdraw money
let withdrawresult = cashRegister.tryWidthdraw(1123.17);
console.log('withdraw successfull', withdrawresult);

// retrieving the state of cashregister
let state = cashRegister.getState();
console.log('current state: ', state);

*/
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console