Cash Register Project case #6

Good morning! I’ve searched through a whole bunch of other posts related to this but I can’t seem to pinpoint my issue. For test case #6, I am getting the correct answer in my Chrome DevTools console. Also at one point I had test cases #1, 2, 4 working and I am just so lost right now I don’t even know what I’m looking at. So if anyone could give me a few pointers or a hint at an area where I’ve gone wrong, I’d appreciate it.

This is what I’m seeing in my console for #6.

  1. {status: “CLOSED”, change: Array(9)}

  2. change: (9) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]

  3. status: “CLOSED”

  4. proto: Object

  5. change: Array(9)

  6. 0: (2) [“PENNY”, 0.5]

  7. 1: (2) [“NICKEL”, 0]

  8. 2: (2) [“DIME”, 0]

  9. 3: (2) [“QUARTER”, 0]

  10. 4: (2) [“ONE”, 0]

  11. 5: (2) [“FIVE”, 0]

  12. 6: (2) [“TEN”, 0]

  13. 7: (2) [“TWENTY”, 0]

  14. 8: (2) [“ONE HUNDRED”, 0]


  let cashRegisterStatus = { status: null, change: null};



let valueOnly = [];


function checkCashRegister(price, cash, cashInDrawer) {

let total = getCashInDrawer(cashInDrawer);
console.log("The total amount in the drawer is $" + total);

let custChange = parseFloat(cash-price).toFixed(2);
console.log("Customer needs $" + custChange + " in change");

let giveThisToCust = getCustChange(custChange, cashInDrawer);
//calling getCustChange function to be used in checkCashRegister function


if(Number(giveThisToCust) < 0 && Number(custChange) > 0) {
  cashRegisterStatus.status = "INSUFFICIENT_FUNDS";
  cashRegisterStatus.change = [];
  console.log(cashRegisterStatus);
  return cashRegisterStatus;
}
if (Number(custChange) > Number(total)) {
  cashRegisterStatus.status = "INSUFFICIENT_FUNDS";
  cashRegisterStatus.change = [];
  console.log(cashRegisterStatus);
  return cashRegisterStatus;
}

if (Number(custChange) === Number(total)) {
  console.log(custChange);
  console.log(total);
  cashRegisterStatus.status = "CLOSED";
  cashRegisterStatus.change=[...cashInDrawer];
  console.log(cashRegisterStatus);
  return cashRegisterStatus;
}



else {
cashRegisterStatus.status = "OPEN";
cashRegisterStatus.change = [...giveThisToCust];
console.log(cashRegisterStatus);
return cashRegisterStatus;

}


} //end checkCashRegister function


function getCashInDrawer (cashInDrawer) {

  let available = 0;

  for (let i = 0; i < cashInDrawer.length; i++) {
    valueOnly.push(cashInDrawer[i][1]);
  available = valueOnly.reduce((total, amount) => total + amount).toFixed(2);

}

return available;
}



function getCustChange(custChange, cashInDrawer){

let change = [];

const denomDict = { //holding all values for each money type
    "PENNY": .01,
    "NICKEL": .05,
    "DIME": .10,
    "QUARTER": .25,
    "ONE": 1.00,
    "FIVE": 5.00,
    "TEN": 10.00,
    "TWENTY": 20.00,
    "ONE HUNDRED": 100.00
  };

  for( let i = cashInDrawer.length - 1; i >= 0; i--){ //looping backwards through the denomDict
    const coinName = cashInDrawer[i][0]; //[i] is looped element, 0 is name of money type
    console.log("This is name of coin " + coinName);
    const coinTotal = cashInDrawer[i][1]; // [i] is looped element, 1 is value of money type
    console.log("How much of all coin type?" + coinTotal);

    const coinValue = denomDict[coinName];//using value of coinName (name of money) and matching to name in denomDict
    console.log("This is the value of the coin " + coinValue);
    let coinAmount = (coinTotal / coinValue ).toFixed(2);
    console.log("How many coins?" + coinAmount);
    let returnedCoins = 0;


    while (custChange >= coinValue && coinAmount > 0) { //while we still need change and coinAmount is greater than 0
      custChange -= coinValue;
      custChange = custChange.toFixed(2);
      coinAmount --;  //amount of coins left in the drawer
      returnedCoins ++; //add to amount return, add 1


}



if(returnedCoins > 0) {
      change.push([coinName, returnedCoins * coinValue]);
      ;// pushing name of the coin "PENNY", plus the amount of change in pennies (value of coin * quantity of coin)
    }




}
console.log(change);
return change;

}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

Well, one thing to note: in your first if(...) branch, you check if Number(giveThisToCust) > 0 … but giveThisToCust is an array, and Number(giveThisToCust) will return not a number (NaN).

I truly like your approach of creating functions to handle the parts, makes a LOT of sense. One thing I might consider, though – all of your various if(...) branches have these two lines:

  console.log(cashRegisterStatus);
  return cashRegisterStatus;

Consider moving those AFTER the if(...) branches. Use the if statement to set the value of cashRegisterStatus, but in every case you return it? Simply let every branch fall through, and return in a single exit point.

So, in your getCustChange(), I think you may be a little confused. It seems you think that, by decrementing custChange here, you’re decrementing custChange back in your main function. Not so. the parameter custChange exists independently of the variable custChange. Wow. Confusing.

The point is this: when your getCustChange() returns, it returns an array of coins due the customer, but you have no way of knowing if, in fact, there is an outstanding balance of change due the customer. To see what I mean, at the end that function (where you console.log(change), try changing that to console.log(custChange, change) – you’ll see some occasions where custChange is not zero – and in that case, you should throw 'INSUFFICENT FUNDS`.

How can you do that? Perhaps have your getCustChange() return an object? If it returned an object that looked like:

{
  amountDue: custChange,
  change: change
}

… then your first branch of the if(...) could simply check if giveThisToCust.amountDue is greater than zero. In this case, getCustChange() couldn’t find exact change.

For the record, making this change, and moving the return out of the if statements are the ONLY changes I made, and your code passed. Just saying.

To the rescue again! Thank you so much. I’ll spend some more time later today going over everything! I appreciate you taking the time to take a look.

Delighted to help, whenever I can. Warmest regards on a snowy new england afternoon. O.o