# JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
My code won’t pass the tests with “OPEN” result. I’m not sure where went wrong. Can someone please help me. Thank you!!!

``````function checkCashRegister(price, cash, cid) {
// set up
let lookup = [['ONE HUNDRED', 100], ['TWENTY', 20], ['TEN', 10], ['FIVE', 5], ['ONE', 1], ['QUARTER', 0.25], ['DIME', 0.1], ['NICKEL', 0.05], ['PENNY', 0.01]];
var change = cash - price;
var originalChange = change;
const reversedCid = cid.reverse(); // reverse cid so the currency units match with lookup

let copyCid = [...reversedCid]; // only make changes to copyCid in later codes

// calculate the initial total amount of cash-in-drawer
var totalCid = 0;
for (let i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}

// different possibilities with changes
let result = [...lookup]; // for later on changing the actual changes for each currency
for (let i = 0; i < result.length; i++) {
let moneyReturned = 0;
let bill = copyCid[i][1] / result[i][1]
bill.toFixed(2);
while (moneyReturned.toFixed(2) >= result[i][1] && bill >= 1) { // when the currency is appropriate for change and there are bills for that currency
change -= result[i][1]; // update the rest of changing amount
moneyReturned += result[i][1]; // update the total amount that has been changed
bill--; // reduce the number of bills by 1
}
//as long as there are still bills for that currency, we can keep changing that same currency till it either runs out or become too big for the rest of the changing amount

// deal with the JS rounding issue here:
if (moneyReturned > 0) {
if (moneyReturned - Math.floor(moneyReturned) !== 0) {
result[i][1] = moneyReturned.toFixed(2); // toFixed method returns a string
result[i][1] = parseFloat(result[i][1]); // we need to convert the string back to a number to pass the tests
}
else {
result[i][1] = moneyReturned;
}
}
else { // when moneyReturned == 0
result[i][1] = moneyReturned; // if no money was returned for that currency, then result[i][1] would be 0
}
}
// after iterating through all the currencies for change
let sumResult = 0;
for (let j = 0; j < lookup.length; j++) {
sumResult += result[j][1];
}
sumResult = sumResult.toFixed(2);

// three outcomes:
if (totalCid == originalChange) {
return {status: "CLOSED", change: cid.reverse()};
}
else if (totalCid < originalChange || sumResult < originalChange) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
else {
let resultFiltered = [];
for (let k = 0; k < result.length; k++) {
if (result[k][1] !== 0) {
resultFiltered.push(result[k])
}
}
return {status: "OPEN", change: resultFiltered}
}
}

console.log(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]]));
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36`

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

to debug, I started by using console.log to track what your function is returning for one of the failing test cases.

``````console.log(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]]));
``````

``````{ status: 'INSUFFICIENT_FUNDS', change: [] }
``````

But the test wants you to return the following:

``````{status: "OPEN", change: [["QUARTER", 0.5]]}
``````

So let’s see what is different and try to figure out why.

First thing is the status. Yours is INSUFFICIENT_FUNDS and theirs is OPEN.
How is the status determined?

Second this is the change. Yours is `change: []` and theirs is ` change: [["QUARTER", 0.5]]`
How is the change calculated for this test case?
So obviously this testcase says the first param is 19.5 and the second is 20, and given this we know that the difference is 50 cents. So we should have some change.

So try to take a look at your code by adding some console.log statements and see if you can track down where things are going wrong.

1 Like

I haven’t dug to deep into the problem, i have a headache right now, but i would recommend not using `var` at this point at all. And in some places you think you are making a clean copy of some things, but you are not. Look up copy by reference vs. value, then look into the spread operator and tell me if you see any problem with how you are using it, and the data you are using it on.

I would recommend tossing some console.logs around with the test cases that are failing and see if you are getting the results you expect.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.