Hi,
I am struggling with the JavaScript Cash Register. My loop works for a couple of iterations but then the results start becoming unexpected. So I guess my logic is bad but I don’t know how to fix it. I’ve been trying for weeks so would really appreciate any help you can provide. Thank you.
The test I am trying to pass is this one:
checkCashRegister(3.26, 100, [
[“PENNY”, 1.01],
[“NICKEL”, 2.05],
[“DIME”, 3.1],
[“QUARTER”, 4.25],
[“ONE”, 90],
[“FIVE”, 55],
[“TEN”, 20],
[“TWENTY”, 60],
[“ONE HUNDRED”, 100],
]);
The test should return the following:
{status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}
My code returns:
[‘TWENTY’, 60], [‘TEN’, 20], [‘FIVE’, 5], [‘FIVE’, 10], [‘FIVE’, 15], [‘ONE’, 1], [‘QUARTER’, 0.25], [‘QUARTER’, 0.5], [‘DIME’, 0.1], [‘DIME’, 0], [‘PENNY’, 0.01], [‘PENNY’, 0.02], [‘PENNY’, 0.03], [‘PENNY’, 0.04]
The TWENTY 60 and TEN 20 match up but then it starts going wrong.
My code:
function checkCashRegister(price, cash, cid) {
console.log(`price is: ${price}`);
console.log(`cash given is: ${cash}`);
// ========= Total In Drawer ======================
let cidTotalTimesOneHundred = 0;
let cidArrayInCents = [];
for (let i = 0; i < cid.length; i++) {
let cidTimesOneHundred = cid[i][1] * 100; // Here we are getting the cash values from the cid (cash in drawer) array so that we can add them up. We multiply each number by 100 because JavaScript doesn't handle numbers with decimal points very well. Then we divide by 100 later to get the numbers back to normal
cidTotalTimesOneHundred = cidTotalTimesOneHundred + cidTimesOneHundred; // Adding up the cid (cash in drawer)
cidArrayInCents.push([cid[i][0], cidTimesOneHundred.toFixed(2)]); // toFixed(2) limits numbers to 2 decimal places
}
let cidTotal = cidTotalTimesOneHundred / 100; // dividing the cid total so that it isn't 100 times too big
console.log(`money in drawer total is: ${cidTotal}`);
// =============== Change Due =============================
let changeTimesOneHundred = cash * 100 - price * 100;
let change = changeTimesOneHundred / 100;
console.log(`change due is: ${change}`);
// =============== Return Change =============================
if (cidTotal < change) {
console.log({ status: "INSUFFICIENT_FUNDS", change: [] });
return { status: "INSUFFICIENT_FUNDS", change: [] };
} else if (cidTotal === change) {
console.log({ status: "CLOSED", change: cid });
return { status: "CLOSED", change: cid };
} else {
const comparisonArray = [
["ONE HUNDRED", 10000],
["TWENTY", 6000],
["TEN", 2000],
["FIVE", 500],
["ONE", 100],
["QUARTER", 25],
["DIME", 10],
["NICKEL", 5],
["PENNY", 1],
];
cid = cid.reverse();
cidArrayInCents = cidArrayInCents.reverse();
// ============ trying again ====================================
let newArr = [];
for (let i = 0; i < comparisonArray.length; i++) {
let value = 0;
while (
// while change is greater than than or equal to the amounts in the comparisonArray array:
changeTimesOneHundred >= comparisonArray[i][1] &&
changeTimesOneHundred > 0
) {
console.log(`changeTimesOneHundred is ${changeTimesOneHundred}`);
// minus the current iteration amount in comparisonArray from the change due:
changeTimesOneHundred = changeTimesOneHundred - comparisonArray[i][1];
value = value + comparisonArray[i][1] / 100;
console.log(`comparisonArray[i][1] is ${comparisonArray[i][1]}`);
console.log(`value times one hundred is ${value * 100}`);
if (value !== 0) {
newArr.push([comparisonArray[i][0], value]);
}
}
}
console.log(`{status: "OPEN", change: ${newArr}}`);
return { status: "OPEN", change: newArr };
}
}