**Tell us what’s happening:
Howdy Y’all!! I have been working on this Check Cash Register Project, does this approach make any sense?
I was considering each calculation from given array(which I transformed to an Object called {availableUnits}) and reducing it by 1 for simplicity in each iterative calculation.
**
Your code so far
function checkCashRegister(price, cash, cid) {
let billsChange;
let currencyUnits = {
PENNY: .01,
NICKEL: .05,
DIME: .1,
QUARTER: .25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100
};
billsChange = cash - price;
let availableUnits = {};
let funds = 0;
let inFunds = { status: "Insufficient Funds", change: [] };
let closed = { status: "Closed", change: [] };
let open = { status: "Open", change: [] };
let result;
cid.forEach(item => {
let key = item[0];
availableUnits[key] = item[1];
funds += availableUnits[key];
});
funds = +funds.toFixed(2);
let units = [];
// console.log("checked");
if (funds < billsChange) {
result = inFunds;
// break;
} else {
let tempFigs = Object.values(currencyUnits);
let temps = Object.values(cid);
let tempVals = Object.values(temps);
tempVals.sort((a, b) => b - a);
let testVals = Object.values(tempVals);
// console.log(",,,,", testVals[0][0], testVals[0][1]);
// console.log(tempFigs, tempVals)
tempFigs.sort((a, b) => b - a);
tempFigs.forEach(item => item <= billsChange ? units.push(item) : -1);
units = units.sort((a, b) => b - a);
let tempNames = [];
tempVals.forEach((item, idx) => {
if (tempFigs[idx] <= billsChange) {
tempNames.push(tempVals[idx][0]);
}
});
tempVals.forEach((item, idx) => {
if (tempNames[0] === item[0]) {
while (billsChange > 0 && billsChange < units[0]*item[1]) {
if (billsChange >= units[0]) {
billsChange -= units[0];
availableUnits[tempNames[0]] -= 1;
closed.change.push(item[0], availableUnits[tempNames[0]]);
result = closed;
} else {
// console.log("here",billsChange, units[0]);
units.splice(0, 1);
tempNames.splice(0,1);
billsChange -= units[0];
availableUnits[tempNames[0]] -= 1; closed.change.push(tempNames[0], availableUnits[tempNames[0]]);
if(billsChange === 0) {
result = closed;
} else {
result = open;
}
// console.log("after",billsChange, units[0]);
}
}
}
});
}
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]]);
:Looking forward to your thoughts or commenyts on this, any kind of feedback is highly welcome.
Thank you.
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: