function checkCashRegister(price, cash, cid) {
let change = []; //
let changeSum = cash - price;
let cidSum = 0;
let cidEach = [ ];
let cidName = [];
cid.reverse().forEach(([key, value]) => {
cidSum += value; cidEach.push(value); cidName.push(key);
});
cidSum = cidSum.toFixed(2);
if (changeSum > cidSum) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
else if (changeSum - cidSum===0 ||changeSum ===0) {
change = cid.reverse();
return { status: "CLOSED", change:change };
}
else {
let unit = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let result = 0;
for (let i = 0; i < unit.length; i++) {
result = parseInt(changeSum / unit[i]);
if (result >= 1) {
if(cidEach[i]>=result*unit[i]){
changeSum = changeSum - result*unit[i];
change.push([cidName[i], result*unit[i]]);
}
else{
changeSum = changeSum - cidEach[i];
change.push([cidName[i], cidEach[i]]);
}
}
}
if(changeSum > 0){
return { status: "INSUFFICIENT_FUNDS", change: [] }
}else{
return { status: "OPEN", change:change };
}
}
}
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]]);
Hi there and welcome to the forum!
Did you have a question about this project?
I have spoilered your code for those who haven’t yet tackled this project.
Also, when you wish to ask for help, it’s easier to click the ‘Get Help’ button and use the built-in Ask for Help feature, which will automatically create a forum post for you which includes your full code, a link to the challenge and an opportunity for you to explain what you are having trouble with.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.