Tell us what’s happening:
I’m trying to move this “while” loop to look for next available “cid” currency to get (balance) change from, but it’s not, after first currencyUnit(e.g. TWENTY). How do I do that?
I have tried an else block with splice method but it seems to be returning an empty array!! Can anyone help, thanks!!
Your code so far
function checkCashRegister(price, cash, cid) {
let billsChange;
// return billsChange;
let currencyUnits = {
PENNY: .01,
NICKEL: .05,
DIME: .1,
QUARTER: .25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100
};
let noFunds = { status: "INSUFFICIENT_FUNDS", change: [] };
let closed = { status: "Closed", change: [] };
let open = { status: "Open", change: [] };
let result;
let availableCashInDrawer = 0;
cid.forEach(item => availableCashInDrawer += item[1]);
billsChange = cash - price;
if (billsChange > availableCashInDrawer) {
result = noFunds;
} else if (billsChange > 0) {
let units = [];
let keys = [];
for (let key in currencyUnits) {
if (currencyUnits[key] <= billsChange) {
units.push(currencyUnits[key]);
keys.push(key);
}
}
units.sort((a, b) => b - a);
keys.sort((a, b) => b - a);
// console.log(units, keys)
cid.forEach(item => {
if (item[0] === keys[0] && billsChange <= item[1]) {
console.log(item[0], keys[0]);
open.change.push([item[0], billsChange]);
} else if (item[0] === keys[0] && item[1] > 0) {
while (billsChange >= 0 && item[1] > 0) {
billsChange -= units[0];
// item[1] -= units[0];
// open.change.push([item[0], item[1]]);
}
open.change.push([item[0], item[1]]);
}
});
result = open;
} else {
cid.forEach(item => {
closed.change.push([item[0], item[1]]);
});
result = closed;
}
return result;
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0
.
Challenge: Cash Register
Link to the challenge: