Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                // 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

Console