Skip to content

Instantly share code, notes, and snippets.

@nickihastings
Last active November 27, 2021 15:20
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 nickihastings/2133fe290046065e7daccfd5c87b2954 to your computer and use it in GitHub Desktop.
Save nickihastings/2133fe290046065e7daccfd5c87b2954 to your computer and use it in GitHub Desktop.
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. Return the string "Insufficient Funds" if cash-in-drawer is less than the change due. Return the…
/* UPDATED NOVEMBER 2021 to work with the latest FreeCodeCamp tests
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price),
payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
cid is a 2D array listing available currency.
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return
the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order,
as the value of the change key.
*/
function checkCashRegister(price, cash, cid) {
//change all decimals to whole numbers for ease
//First work out how much change is needed
var change = (cash * 100) - (price * 100);
var currentStatus = '';
//Next work out how much cash there is in the cash register using
//reduce to add all the values and turn to whole numbers
var totalCash = cid.reduce(function(accum, current){
accum = accum + (current[1]*100);
return accum;
}, 0);
//Next set up the coins array/object with empty values,
//and the coin denomination, e.g Penny = 1 and dollar = 100 pennies
var coins = [
{name: "ONE HUNDRED", value: 0, denomination: 10000},
{name: "TWENTY", value: 0, denomination: 2000},
{name: "TEN", value: 0, denomination: 1000},
{name: "FIVE", value: 0, denomination: 500},
{name: "ONE", value: 0, denomination: 100},
{name: "QUARTER", value: 0, denomination: 25},
{name: "DIME", value: 0, denomination: 10},
{name: "NICKEL", value: 0, denomination: 5},
{name: "PENNY", value: 0, denomination: 1},
];
//from the info passed to the function add the values for each coin into
//the cash register objects.
coins.map(function(element){
for(var i = 0; i < cid.length; i++){
if(cid[i][0] === element.name){
element.value = parseFloat(Number(cid[i][1]*100).toFixed()); //remove decimals
}
}
});
//to work out the change/number of coins to be given as change
//set up a variable to hold the new array
var changeDue = [];
//Check that there is enough change in the register
if(change > totalCash){
currentStatus = "INSUFFICIENT_FUNDS";
}
//if all change is returned close the register
else if(change === totalCash){
currentStatus = "CLOSED";
// loop over the coins to return the cash in drawer
for(var cs = 0; cs<coins.length; cs++){
var name = coins[cs].name;
var value = coins[cs].value;
changeDue.push([name, value/100]);
}
//reverse the object so that lowest denomination is first
cash = changeDue.reverse();
//and return the result
return {status: currentStatus, change: cash};
}
//there is change...
else{
currentStatus = "OPEN";
}
//loop over the coins array of objects and check coin values against the change
//needed.
for(var j = 0; j<coins.length; j++){
var name = coins[j].name;
var value = coins[j].value;
var denom = coins[j].denomination;
//if change can be divided by the coins denomination and is greater than one coin
//and the value of the coins is not 0 and change is not 0
if(change/denom >=1 && value > denom && change !== 0){
var coinNum = Math.floor(value/denom); //the number of coins there are
var amount = Math.floor(change/denom); //the number of coins needed
//if there are more coins than needed the actual value is the number of coins
//multiplied by the coin denomination (e.g. 5 x 25p).
if(coinNum > amount){
amount = amount * denom;
}
else{ //else take the full value
amount = coinNum * denom;
}
change = change - amount; //reduce the change by the value taken out
amount = parseFloat(Number(amount/100).toFixed(2)); //decimalize the amount
changeDue.push([name, amount]); //add to array
}
}
//if there wasn't enough change in the correct coins:
if(changeDue.length === 0){
currentStatus = "INSUFFICIENT_FUNDS";
}
// Here is your change, ma'am.
return {status: currentStatus, change: changeDue};
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
@rlniranjan9
Copy link

not working

@coffeeguy01
Copy link

@rlniranjan9 is correct that this solution does not pass the current freecodecamp.com challenge. A great opportunity to investigate this script which does 99% of the heavy lifting.

@ngimdock
Copy link

not workink

@coffeeguy01
Copy link

not workink

Yes. Investigate it and decide for yourself the solution. This code does most of the work.

@nickihastings
Copy link
Author

Thanks for the comments @coffeeguy01 and @ngimdock, I have now updated the script to run with the latest FreeCodeCamp tests. (This was originally done a while ago.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment