Banging My Head Against the Cash Register

Tell us what’s happening:
I believe I have come up with a sound solution to the Cash Register problem; however, I can’t pass it even though its outputting the correct values. I think maybe its related to the spaces between brackets in the change array … but I can’t figure out why that is happening or if it’s even the reason.

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

my code returns { status: 'OPEN', change: [ [ 'QUARTER', '0.5' ] ] }
   **Your code so far**

function checkCashRegister(price, cash, cid) {

let changeDue = (cash - price); //Need to know how much change we need to give back
let remainingDue = changeDue; //While we are giving change we should know how much more change we need to give after giving out some of the change
let changeGiven = 0; //We should know how much chnage we have given out at any time
const originalDrawer = JSON.parse(JSON.stringify(cid)).reverse(); //keep original drawer for comparing
const cashInDrawer = JSON.parse(JSON.stringify(originalDrawer)); //working drawer
const output = {status: '', change: []}; //drawer output status and change count
let cidTotal = cashInDrawer.reduce((sum, money) => sum + money[1], 0); //Total cash in the drawer

//Set values for each unit of currency
var values = {
    "PENNY":       0.01,
    "NICKEL":      0.05,
    "DIME":        0.10,
    "QUARTER":     0.25,
    "ONE":         1.00,
    "FIVE":        5.00,
    "TEN":         10.00,
    "TWENTY":      20.00,
    "ONE HUNDRED": 100.00
  }

//Use this function to make change
function makeChange(){

// if we have enough change in the drawer 
if (changeDue < cidTotal) { 
output.status = "OPEN"; //set status to OPEN

//Loop through the cash in the drawer
for (let a = 0; a < cashInDrawer.length; a++){

//do this as long as we have not remaining change to be given
  if (remainingDue > 0) { 

//while each unit amount is dividable by its currency value with no remainder and the amount of the unit is not 0    
    while (remainingDue.toFixed(2)/values[cashInDrawer[a][0]] >=1 && cashInDrawer[a][1]>0){ 

  cashInDrawer[a][1] -= values[cashInDrawer[a][0]]; //subtract the unit value from total value of unit in the drawer
  remainingDue -= values[cashInDrawer[a][0]]; //subtract unit value from remaining change
  changeGiven += values[cashInDrawer[a][0]]; //add unit value to change handed out
  }
 }
 //if the cash between the drawers is not zero push units and values to output
 if (originalDrawer[a][1] - cashInDrawer[a][1] !== 0){ 
 output.change.push([cashInDrawer[a][0],Math.round((originalDrawer[a][1] - cashInDrawer[a][1])*100)/100]);
 }
} 
//If the change in the drawer is exact status to closed and output cashInDrawer
} else if (changeDue == cidTotal) {
  output.status = "CLOSED";
  for (let b = cashInDrawer.length-1; b >= 0; b--){
  output.change.push(cashInDrawer[b][0],cashInDrawer[b][1]);
  }
//if the change due is more than whats in the drawer set status to insufficient funds
} else if (changeDue > cidTotal) {
  output.status = "INSUFFICIENT FUNDS"; 
} 
return output;
}

console.log({changeDue});
console.log({remainingDue});
console.log({changeGiven});
console.log(makeChange());
return makeChange();
}

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]]);
   **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

1 Like

Try commenting out the following line:

console.log(makeChange());

And then see what happens. You’re still going to get some tests failed, but not the one you referenced above. Can you explain why?

P.S. It has nothing to do with the formatting of the object when printed to the console.

2 Likes

Thank you so much for the help!

I commented out the console.log(makeChange()); like you said which helped me see the other issues in my code. As for why the console.log(makeChange()); was causing part of the failing issue, I don’t know exactly why. I googled a bit and didn’t see much; however, my best guess would be that even though I was “printing/logging” the solution to the console because it was a function was it actually returning values?

The other issues in my code included not accounting for the issue where I couldn’t make change with the cash in the drawer (like when I needed to give 50 cents worth of change but only had a dollar and a penny). I corrected this after getting past the flood of failures caused by the console.log(makeChange());

Could you help me understand the console.log issue?

Here’s what I ended up with - so happy for the help and for acquiring this certificate! Been working on this one for 3 days!

function checkCashRegister(price, cash, cid) {

let changeDue = cash - price; //Need to know how much change we need to give back
let remainingDue = changeDue; //While we are giving change we should know how much more change we need to give after giving out some of the change
let changeGiven = 0.00; //We should know how much chnage we have given out at any time
const originalDrawer = JSON.parse(JSON.stringify(cid)).reverse(); //keep original drawer for comparing
const cashInDrawer = JSON.parse(JSON.stringify(originalDrawer)); //working drawer
const output = {status: '', change: []}; //drawer output status and change count
let cidTotal = cashInDrawer.reduce((sum, money) => sum + money[1], 0); //Total cash in the drawer

//Set values for each unit of currency
var values = {
    "PENNY":       0.01,
    "NICKEL":      0.05,
    "DIME":        0.10,
    "QUARTER":     0.25,
    "ONE":         1.00,
    "FIVE":        5.00,
    "TEN":         10.00,
    "TWENTY":      20.00,
    "ONE HUNDRED": 100.00
  }

//Use this function to make change
function makeChange(){

// if we have enough change in the drawer 
if (changeDue < cidTotal) { 
output.status = "OPEN"; //set status to OPEN

//Loop through the cash in the drawer
for (let a = 0; a < cashInDrawer.length; a++){

//do this as long as we have not remaining change to be given
  if (remainingDue > 0) { 

//while each unit amount is dividable by its currency value with no remainder and the amount of the unit is not 0    
    while (remainingDue.toFixed(2)/values[cashInDrawer[a][0]] >= 1 && cashInDrawer[a][1]>0){ 

  cashInDrawer[a][1] -= values[cashInDrawer[a][0]]; //subtract the unit value from total value of unit in the drawer
  remainingDue -= values[cashInDrawer[a][0]]; //subtract unit value from remaining change
  changeGiven += values[cashInDrawer[a][0]]; //add unit value to change handed out
  }
 }
 //if the cash between the drawers is not zero push units and values to output
 if (originalDrawer[a][1] - cashInDrawer[a][1] !== 0){ 
 output.change.push([cashInDrawer[a][0],Math.round((originalDrawer[a][1] - cashInDrawer[a][1])*100)/100]);
 } 
} if(changeDue > changeGiven) {
  console.log({changeGiven},{changeDue})
  output.status = "INSUFFICIENT_FUNDS";
  output.change = [];
  return output;
  }
//If the change in the drawer is exact status to closed and output cashInDrawer
} else if (changeDue == cidTotal) {
  output.status = "CLOSED";
  for (let b = cashInDrawer.length-1; b >= 0; b--){
  output.change.push([cashInDrawer[b][0],cashInDrawer[b][1]]);
  }
  //return output;
//if the change due is more than whats in the drawer set status to insufficient funds
} else if (changeDue > cidTotal) {
  output.status = "INSUFFICIENT_FUNDS";
} 
return output;
}
//console.log(makeChange());
return makeChange();
}

Let me ask you this. What is the scope of the variable cidTotal in relation to the function makeChange()?

Alright - taking a stab at this:
Is the issue that cidTotal is using cashInDrawer as a reference? If I call the function once ,whether its in console.log or wherever, the cashInDrawer variable has been mutated by that function already. So when I call it again, the cidTotal and cashInDrawer variable is not the same as the first time called?

Exactly, because all of these variable are global with respect to the makeChange() function then any changes to them will be retained each time you call makeChange() and thus the second time you called it the values weren’t what you were expecting them to be.

1 Like

Thank you for all of the help! I knew they were global variables as I intentionally did that; but, for some reason I didn’t consider that in the end when I wasn’t getting the expected outcome.

Again, really appreciate the time you spent helping me understand. It is invaluable to me on my journey to learning to code and switching careers.

This sort of thing is a good example of why it is a good idea to make the scope of each variable as small as possible.