## Freecode camp checkCashRegister problem.

I did not like the solution to this problem. But here it is., the question was about giving exact change back to user. The probiem with a \$100 spent for a \$3.26 purchase.

``````
var changeGiven = [];
var total = 0;
var quarters = 0;
var pennies = 0;
var nickels = 0;
var dimes = 0;
for (var totalCount = 0; totalCount < cid.length; totalCount++) {
total = total + cid[totalCount];
if (cid[totalCount] === "QUARTER") {
quarters = Math.round((cid[totalCount] * 100 / 25), 2);
} else if (cid[totalCount] === "PENNY") {
pennies = Math.round((cid[totalCount] * 100 / 1), 2);
} else if (cid[totalCount] === "NICKEL") {
nickels = Math.round((cid[totalCount] * 100 / 5), 2);
} else if (cid[totalCount] === "DIME") {
dimes = Math.round((cid[totalCount] * 100 / 10), 2);
}
}
return changeGiven
```
```

The above block solves most of the items that is needed to solve the problem. But when we are working on the \$100 problem, where cashRegister had to return some definite values., here is the code snippet for that.

``````
var changeToGive = cash - price;
runningChange = changeToGive;
var runningChange;
var twenties;
if (total - changeToGive == 0) {
return "Closed";
} else if (total - changeToGive < 1) {
return "Insufficient Funds";
}

if ((changeToGive  1 && changeToGive > 20) {
twenties = cid.find(function (element) {
return element == "TWENTY";
});
if (changeToGive > twenties) {
runningChange = parseFloat(
Math.round((runningChange - twenties) * 100) / 100).toFixed(
2);
}
changeToGive = runningChange;
if (runningChange > 0) {
changeGiven.push([twenties, parseFloat(twenties.toFixed(2))]);
}

}
var tens;
if (changeToGive > 1 && changeToGive > 10) {
tens = cid.find(function (element) {
return element == "TEN";
});
if (changeToGive > tens) {
runningChange = parseFloat(
Math.round((runningChange - tens) * 100) / 100).toFixed(2);
}
changeToGive = runningChange;
if (runningChange > 0) {
changeGiven.push([tens, parseFloat(tens.toFixed(2))]);
}

}
var fives;
if (runningChange > 1 && changeToGive > 5) {
fives = cid.find(function (element) {
return element == "FIVE";
});
if (changeToGive > fives) {
console.log("Fives more" + fives);
}
runningChange = parseFloat(
Math.round((runningChange - fives) * 100) / 100).toFixed(2);
if (runningChange > 1) {
changeGiven.push([fives, parseFloat(fives.toFixed(2))]);
} else {
runningChange = parseFloat(
runningChange - Math.floor(runningChange / 5) * 5).toFixed(2);
var temp = ((Math.floor(changeToGive / 5) * 5) / 5);
changeToGive = runningChange;
changeGiven.push(["FIVE", parseFloat((temp * 5).toFixed(2))]);
console.log(changeGiven);
}
}
var ones;
if (runningChange > 1 && changeToGive > 1) {
ones = cid.find(function (element) {
return element == "ONE";
});
changeGiven.push(
["ONE", parseFloat(Math.floor(runningChange).toFixed(2))]);
runningChange = runningChange - Math.floor(runningChange);
changeToGive = runningChange;
console.log(changeGiven);
}
if (runningChange * 100 > 24 || ((runningChange * 100)) % 25 == 0) {
changeGiven.push(["QUARTER",
parseFloat(Math.floor(runningChange / 0.25) * .25.toFixed(2))]);
runningChange = runningChange - Math.floor(runningChange / 0.25) * .25;
changeToGive = runningChange;
console.log(changeGiven);
}
if (runningChange * 100 > 10) {
changeGiven.push(["DIME",
parseFloat((Math.floor(runningChange / 0.10) * .10).toFixed(2))]);
runningChange = runningChange - Math.floor(runningChange / 0.10) * .10;
changeGiven.push(["PENNY", parseFloat(runningChange.toFixed(2))]);
console.log(changeGiven);
}
```
```

So first we calculated how much money we have to return from a cash register. Then we have three code blocks for the dollar denominations. Each block we check if the amount returned is greater than the money inside cashRegister.
The tricky part was the calculation of the last .76 cents. We first found out how many quarters we have in that denomination. Then went to the nickel and then to dimes.
Finally, the solution needed everything in common float blocks. So before adding to the final array, we made a sanity check to ensure all amounts are on a float.