Skip to content

Instantly share code, notes, and snippets.

@AndSheCodes2
Created October 8, 2019 03:44
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 AndSheCodes2/50c64dcfdf8b5739b164abf6543dc7c9 to your computer and use it in GitHub Desktop.
Save AndSheCodes2/50c64dcfdf8b5739b164abf6543dc7c9 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kiluzov
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdn.rawgit.com/zloirock/core-js/master/client/shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.0.8/es5-shim.min.js"></script>
<script id="jsbin-javascript">
// customer pays for item
// cashier change
const money = [ 100.00, 50.00, 20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01];
const giveChange = function (itemPrice, bills){
let change = bills - itemPrice;
// create an object
let exactChange = {};
let moneyLength = money.length;
for(let i = 0; itemPrice > 0 && i < moneyLength; i++ ){
// loop thru array check if change is greater than money
// if change is greater than money subtract value from change
// credit a placeholder for money index
let x = money[i];
if(x <= money[i]) {
exactChange[x] = Math.floor(change / x)
change -= x * exactChange[x];
// console.log(money[i]);
//change = change - money[i];
//exactChange.push(money[i])
}
}
return exactChange;
}
console.log(giveChange(12.50, 20.00));
console.log(giveChange(12.50, 50.00));
</script>
<script id="jsbin-source-javascript" type="text/javascript">// customer pays for item
// cashier change
const money = [ 100.00, 50.00, 20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01];
const giveChange = function (itemPrice, bills){
let change = bills - itemPrice;
// create an object
let exactChange = {};
let moneyLength = money.length;
for(let i = 0; itemPrice > 0 && i < moneyLength; i++ ){
// loop thru array check if change is greater than money
// if change is greater than money subtract value from change
// credit a placeholder for money index
let x = money[i];
if(x <= money[i]) {
exactChange[x] = Math.floor(change / x)
change -= x * exactChange[x];
// console.log(money[i]);
//change = change - money[i];
//exactChange.push(money[i])
}
}
return exactChange;
}
console.log(giveChange(12.50, 20.00));
console.log(giveChange(12.50, 50.00));
</script></body>
</html>
// customer pays for item
// cashier change
const money = [ 100.00, 50.00, 20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01];
const giveChange = function (itemPrice, bills){
let change = bills - itemPrice;
// create an object
let exactChange = {};
let moneyLength = money.length;
for(let i = 0; itemPrice > 0 && i < moneyLength; i++ ){
// loop thru array check if change is greater than money
// if change is greater than money subtract value from change
// credit a placeholder for money index
let x = money[i];
if(x <= money[i]) {
exactChange[x] = Math.floor(change / x)
change -= x * exactChange[x];
// console.log(money[i]);
//change = change - money[i];
//exactChange.push(money[i])
}
}
return exactChange;
}
console.log(giveChange(12.50, 20.00));
console.log(giveChange(12.50, 50.00));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment