# Cash Register Feedback Request

Tell us what’s happening:
Describe your issue in detail here.

Finished the final JS project. I’m glad I was able to do it. Yet, I’m sure there are a lot of improvements that could be made. Overall, it seems cobbled together and disjointed. As always, any feedback is welcome.

In particular,

• While decrementing the amount of change needed, the variable became a long decimal instead of a round number (e.g. 2.04999999999 not 2.05). Why is this? It created a problem where I was off by one cent. I used ` toFixed(2)` in the code, but is there a better way to keep the numbers at two decimals?

• Is there any good way to reduce the number of variables I defined at the top?

• Can I condense the amount of conditionals used?

• Any better ways of determining if there is enough change in the drawer to cover what is needed? Checking the total change needed against the total in the drawer wasn’t enough. There is an exception where the bills in the drawer are too big, so change can’t be given.

Thank you,

E

``````  **Your code so far**
``````
``````
function checkCashRegister(price, cash, cid) {

const moneyValue = {0: ["PENNY", 0.01], 1: ["NICKEL", 0.05], 2: ["DIME", 0.10], 3: ["QUARTER", 0.25], 4: ["ONE", 1.00], 5: ["FIVE", 5.00], 6: ["TEN", 10.00], 7: ["TWENTY", 20.00], 8: ["ONE HUNDRED", 100.00]};

let totalChange = cash - price;  //change needed
let changeInDrawer = cid //copys array to prevent the input from being mutated below
let totalCid = 0; //total money in register
let accume = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]; //stores how much of each individual denom is needed

let status = "" // string to set open/closed/ins fundss
let change = [];  //array to hold final change given

for (let i = 0; i < cid.length; i++) { //finds total change in drawer
totalCid += cid[i][1];
};

totalCid < totalChange ? status = "INSUFFICIENT_FUNDS"       // sets status against totalCid vs totalChange
: totalCid === totalChange ? status = "CLOSED"
: status = "OPEN"

for (let i = 8; i >= 0; i--) {   //builds the "change spread" changeArr, how much of each denomonation should be pulled from the register, loops through each denomonation in the drawer

if (status === "INSUFFICIENT_FUNDS" ) {  // with Insuf Funds cases, the program should not return how much change was given (none was)
change = []
i=0
}
else if (totalChange >= changeInDrawer[i][1]) {  // if the total change needed is more than the amount of a denomonation in the drawer, take it all, update how much change is needed
totalChange -= changeInDrawer[i][1];

} else if (totalChange < changeInDrawer[i][1] && (totalChange/moneyValue[i][1]).toFixed(2) >= 1) { // if the total change needed is less than the denomonation in the drawer, check if at least one bill/coin can indexe pulled from that denomonation

totalChange -= moneyValue[i][1];
changeInDrawer[i][0] -= moneyValue[i][1];

i++  // reset i for that round so the loop checks again, until no more of that denom can be pulled
}
}

changeSpread.map((_denom, index, changeArr) => {   //builds the accum array, which accumulates each denomonation of change into one sub-object

for (let i = 0; i < accume.length; i++) {
if (changeArr[index][0] === accume[i][0]) {
accume[i][1] += changeArr[index][1]
}};
});

if (totalChange > 0) {  //if there is still a value in the total change variable, there was not enough in the register for change, sets insufficent funds conditions
status = "INSUFFICIENT_FUNDS"
change = []
}
if (status !== "CLOSED" && totalChange <= 0) {  // builds the final arrays change output
accume.filter( (x, y, z) => {
if (z[y][1] !== 0) {
change.unshift(x);
}
});
} else if (status === "CLOSED") {    //if status is closed, returns the cid array (which equals the accum array)
change = accume
};

return {status: status, change: change};   //final output
};
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36`

Challenge: Cash Register