Cash Register Feedback, I solved the problem, though I feel like I did it wrong and impostor syndrome is attacking

“Hi Campers, The title says it all, but I still feel weird about my solution. It seems like I should have been able to solve the project with a smarter, more advanced approach. I kept trying to refactor it, but nothing actually seemed as simplistic or made sense to me to use. When I kept thinking about approaching it with using maybe a key/value pair object for the denominations or a reduce method. It just seemed like I didn’t understand how to do it with those approaches or methods.

I honestly am just looking for some type of validation at this point, because something about me solving it the way I did feels like I am not coding it correctly. I really wanted to share my thoughts on this because I feel like I don’t even deserve to be done with this Algorithm and Data Structure section, based on this solution I came up with.

Anyhow is this just impostor syndrome at work and I can move on with this being a valid solution and be happy and confident with what I done, or should I try to continue to solve this project using a better solution?”

> 
> function checkCashRegister(price, cash, cid) {
> 
> //copy arguments into variables.
> 
> var mCash = cash;
> 
> var mPrice = price;
> 
> var mCid = cid;
> 
> //reverse the cash in drawer array to allow to return values in correct order.
> 
> var mCidReverse = cid.reverse();
> 
> var changeTotal = mCash - mPrice;
> 
> //these to variables hold the values of what a currency is worth. //this can also be done with an object.
> 
> var moneyType = ['ONE HUNDRED', 'TWENTY', 'TEN', 'FIVE', 'ONE', 'QUARTER', 'DIME', 'NICKLE', 'PENNY'];
> 
> var moneyAmount = [100.00, 20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01];
> 
> //the array to return change to customer, denomination of money will be pushed here when needed.
> 
> var changeToCustomer = [];
> 
> //Objects to return, these are the 3 types of return values.
> 
> var statusInsufficentFunds = {
> 
> status: "INSUFFICIENT_FUNDS",
> 
> change: []
> 
> };
> 
> var statusOpen = {
> 
> status: "OPEN",
> 
> change: changeToCustomer
> 
> };
> 
> var statusClosed = {
> 
> status: "CLOSED",
> 
> change: mCid
> 
> };
> 
> // Check to see that cash drawer has enough money in it. If it doesn't return insufficient funds, if it is exactly equal return status closed.
> 
> if (registerTotalAmount(mCidReverse) < changeTotal) {
> 
> return statusInsufficentFunds;
> 
> } else if (registerTotalAmount(mCidReverse) == changeTotal) {
> 
> statusClosed.change.reverse();
> 
> return statusClosed;
> 
> }
> 
> //loop through the array of denomination in cash drawer
> 
> for (var i = 0; i < mCidReverse.length; i++) {
> 
> //create a total amount that we are giving back to customer to make sure we have the right funds.
> 
> var total = 0;
> 
> //while the change total we owe the customer is greater than the denomination value and we currently have that denomination funds. Give that amound to the cutomer for their change.
> 
> while (changeTotal >= moneyAmount[i] & mCidReverse[i][1] > 0) {
> 
> //subtract the value amount from the current drawer so we don't overdraft
> 
> mCidReverse[i][1] -= moneyAmount[i];
> 
> //subtract from how much change we still owe.
> 
> changeTotal -= moneyAmount[i];
> 
> //add the amount we just gave back to the total so we have a record of how much we gave.
> 
> total += moneyAmount[i];
> 
> //make sure to have a precise number so we don't have decimal values past 4.
> 
> changeTotal = changeTotal.toPrecision(4);
> 
> }
> 
> //now if we gave back change of that denomination we push the total and that denomination type to an array to return with what change we gave back.
> 
> if (total > 0) {
> 
> changeToCustomer.push([moneyType[i], total]);
> 
> }
> 
> }
> 
> //if we still owe money to the customer because of not having exact change we return insufficent funds. Else we are all good and return status open and change.
> 
> if (changeTotal > 0) {
> 
> return statusInsufficentFunds;
> 
> } else {
> 
> return statusOpen;
> 
> }
> 
> // Here is your change, ma'am.
> 
> }
> 
> // Example cash-in-drawer array:
> 
> // [["PENNY", 1.01],
> 
> // ["NICKEL", 2.05],
> 
> // ["DIME", 3.1],
> 
> // ["QUARTER", 4.25],
> 
> // ["ONE", 90],
> 
> // ["FIVE", 55],
> 
> // ["TEN", 20],
> 
> // ["TWENTY", 60],
> 
> // ["ONE HUNDRED", 100]]
> 
> checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
> 
> function registerTotalAmount(cashInDrawer) {
> 
> //total the amount of money in the register to see if we have enough to go on.
> 
> var totalCid = 0;
> 
> for (var i = 0; i < cashInDrawer.length; i++) {
> 
> totalCid += cashInDrawer[i][1];
> 
> }
> 
> return totalCid;
> 
> }
1 Like