Cash register. Im stuck. Help

Hey there campers! I’m stuck…I don’t seem to understand the part where I have to break the change into dollar bills.
I included my code so far below.
I don’t ask about the solution, I want help understanding. Please guide me to the light at the end of this tunnel. I’ve been trying to wrap my brain around it for days.
I gotta say this one is by far the hardest one yet.

Thanks in advance to everyones help.

function checkCashRegister(price, cash, cid) {

function Status(status, change) {
      this.status = status;
      this.change = change;
  } 

    let moneyUnit = [
      ["ONE HUNDRED", 10000],
      ["TWENTY", 2000],
      ["TEN", 1000],
      ["FIVE", 500],
      ["ONE", 100],
      ["QUARTER", 25],
      ["DIME", 10],
      ["NICKEL", 5],
      ["PENNY", 1]
    ];

    let changeDue = cash*100 - price*100; //change due in pennies
    let totalInDrawer = 0;
    let changeForCustomer = [];

    for (let i = 0; i < cid.length; i++) {
      totalInDrawer += cid[i][1]*100; //total in pennies
    }

    if (totalInDrawer < changeDue) {
      let registerStatus = new Status("INSUFFICIENT_FUNDS", []);
    } else if (totalInDrawer === changeDue) {
        let registerStatus = new Status("CLOSED", cid);
    } else {
      cid = cid.reverse();
        for (let i = 0; i < moneyUnit.length; i++) {
          for (let j = 0; j < cid.length; j++) {
            
            while (changeDue >= moneyUnit[i][1] && cid[j] > 0) {
                
                // ????????????? //
                // ????????????? //
                
            }
          }
        }
    }
}
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]]);

Can you explain in a little more detail what you don’t understand?

So I’m trying to write my pseudo code on a piece of paper. Step by step. And I can’t even put it together in plain English, how to make it take out money from a correct dollar bill compartment.
I have two arrays, one with just money units, and the other one which is getting passed originally. I’m looping through one and looping through the other one. And after a while loop I’m getting confused. How to overlap them now.

Let’s use a real world example.

If you had a cash register with a drawer full of bills and change and someone handed you a twenty to pay for something that costs $6.49, how would you go about figuring out if you could give them exact change for their purchase?

hm…

  • I would look at the dollar bill I was handed,
  • Then I would do the math, to see how much I have to give back,
  • Then open a register and start taking out the dollar bills one by one, but I have to make sure to take out bills smaller than the twenty I was handed

You are on to something here but you need to describe it in more detail. If I gave you a 20 for something that costs $6.49, the change due is $13.51. So what bill would you look for first and why? What if you don’t have any of those in your cash register? If you do, how many of those do you include? Once you’ve included those bills, how do you keep track of how much change you still need to give? How do you figure out what you should do next to continue making change?

Computers aren’t people. You can’t just say “make change for this purchase” and then assume the computer will figure it out. You have to tell a computer exactly what to do in order for it to determine if it can produce the exact change needed. If you were working as a cashier at a store and opened up the cash register to make change, you would need to go through some process in your mind in order to determine how you would make that change. Do you have a 10 you can include in the change? If not, do you have any 5s you can include in the change? And so on. You may not realize you are going through this same process each time, but if you think about it, you are. That’s the process you need to instruct the computer to do. But before you can do that, you need to understand exactly how you are doing it in your own mind. That’s what I’m asking you to describe.

Im looking for the bill which is equal to $13.51, because maybe I can give out the change in one bill.

In this case I have to look for the highest bill to mach the change

I take only one and start looking over again

Probably a variable? Like a accumulator

I thought of deducting each bill from my change due, until it equals to zero (because I wont have any change to give out).

Here is what I mean.

I look through the register drawer for $13.51,
Apparently there is no such bill like $13.51 in the drawer
I have to take a $10, its a highest closest one to $13.51 and I deducted from $13.51.
Now I have $3.51, and I start looking if I have this bill
Again, no such bill.
Lets take $1 and deduct it from $3.51
Now I have $2.51, and Im looking through the drawer again,
Well again Im taking $1 and deduct it from $2.51. and so on until I have zero left.

But I guess instead of deducting I have to accumulate it in the variable I mentioned earlier?

Would you really look for a $13.51 bill in the drawer? I think you know that there is no such thing :slight_smile:

This sounds like a good idea.

I think you are starting to get the idea of explaining in plain English how you go about figuring this out. I think you might be leaving out some details that a computer will need to know, but as you work on your code you’ll probably realize that.

Do you think you have a good enough understanding of how to explain this to a computer? If so then start coding. If not, then please ask more questions.

I know :sweat_smile: Just wanted to be as descriptive as possible.

I definitely do. I really appreciate your guiding questions. That helped a lot.

Yup Im missing something now. Cause it giving a change even though there is no quarters. I assume it’s because the total amount in the drawer is greater than change due. I will try adjusting my conditionals to figure this out.

Oh well. I’m stuck again.

    } else {
      
      cid = cid.reverse();
        for (let i = 0; i < moneyUnit.length; i++) {
          for (let j = 0; j < cid.length; j++) {
            
            let customer = [moneyUnit[i][0], 0]; // my acumulator var

              while (changeDue >= moneyUnit[i][1] && cid[j][1] > 0) {
                
                changeDue -= moneyUnit[i][1];
                customer[1] += moneyUnit[i][1]/100;
                cid[j][1] -= changeDue/100;

              }
              
          if (customer[1] > 0) {
            changeForCustomer.push(customer);
          }
        }
      }
    }
    if (changeDue > 0) {
      //console.log(new Status("INSUFFICIENT_FUNDS", []));
      return new Status("INSUFFICIENT_FUNDS", []);
    }
    console.log(new Status("OPEN", changeForCustomer));
    //return new Status("OPEN", changeForCustomer);
}

the code works. But when there is no quarters in this case, but since there is a dollar bill. It still gives the change, but it should give me a message saying "insufficient funds " instead of taking it out of the dollar.

So my question is: how do I make it stop after it checked that there is no quarters?

You need to paste your entire function in here. Part of helping you is testing it for ourselves and we can’t do that unless we have all of your code.

Attached the function below.

function checkCashRegister(price, cash, cid) {

function Status(status, change) {
      this.status = status;
      this.change = change;
  } 

    let moneyUnit = [
      ["ONE HUNDRED", 10000],
      ["TWENTY", 2000],
      ["TEN", 1000],
      ["FIVE", 500],
      ["ONE", 100],
      ["QUARTER", 25],
      ["DIME", 10],
      ["NICKEL", 5],
      ["PENNY", 1]
    ];

    let changeDue = cash*100 - price*100; //change due in pennies
    let totalInDrawer = 0;
    let changeForCustomer = []; 

    for (let i = 0; i < cid.length; i++) {
      totalInDrawer += cid[i][1]*100; //total in pennies
    }

    if (totalInDrawer < changeDue) {
      let registerStatus = new Status("INSUFFICIENT_FUNDS", []);
    } else if (totalInDrawer === changeDue) {
        let registerStatus = new Status("CLOSED", cid);
    } else {
      
      cid = cid.reverse();
        for (let i = 0; i < moneyUnit.length; i++) {
          for (let j = 0; j < cid.length; j++) {
            
            let customer = [moneyUnit[i][0], 0]; // my acumulator var

              while (changeDue >= moneyUnit[i][1] && cid[j][1] > 0) {
                
                changeDue -= moneyUnit[i][1];
                customer[1] += moneyUnit[i][1]/100;
                cid[j][1] -= changeDue/100;

              }
              
          if (customer[1] > 0) {
            changeForCustomer.push(customer);
          }
        }
      }
    }
    if (changeDue > 0) {
      //console.log(new Status("INSUFFICIENT_FUNDS", []));
      return new Status("INSUFFICIENT_FUNDS", []);
    }
    console.log(new Status("OPEN", changeForCustomer));
    //return new Status("OPEN", changeForCustomer);
}
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

Should this return statement really be commented out?

Definitely not, I commented it out, to be able to see the console.log.

In the future, please make sure to give us your working code so that we don’t have to wonder about these things :slight_smile:

You don’t need that last console.log in your code if you wrap the function call in a console.log itself:

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

Now you will see exactly what the function is returning.

For the first one you are getting wrong (which is the one in the console log above), This is what the function is returning:

{ status: 'OPEN',
  change: 
   [ [ 'TWENTY', 40 ],
     [ 'TWENTY', 40 ],
     [ 'TEN', 10 ],
     [ 'FIVE', 5 ],
     [ 'ONE', 1 ],
     [ 'QUARTER', 0.5 ],
     [ 'DIME', 0.2 ],
     [ 'PENNY', 0.04 ] ] }

First thing I would figure out is why there are two twenties in there.

I’m sorry about that.

Got it

Strange why it’s doing that.

Could that be, because I have a nested “for” loop?

for (let i = 0; i < moneyUnit.length; i++) {
          for (let j = 0; j < cid.length; j++) {

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.