Cash Register struggle

Tell us what’s happening:
I’m starting on the cash register challenge, and to be honest, I have no where near any idea of how to start. I read through all of the hints, and I still cant make heads or tails of anything. Does any one have any resources that could help me figure out a way to do this? I feel as though I have a decent grasp of all the concepts in the JS challenges, but I don’t have the ability to figure out how to put all the pieces together into something that would solve this.

Your code so far

function checkCashRegister(price, cash, cid) {
  var change;
  // Here is your change, ma'am.
  return change;

// 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", 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 (X11; CrOS aarch64 11151.29.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.49 Safari/537.36.

Link to the challenge:

Yeah it’s a tricky one. Take it on piece by piece, one piece at a time. You could start by adding code that relates currency units (“PENNY”, “TEN”, etc) to their corresponding amounts. Various ways you could do this. Then you could, perhaps, find what change is due to the customer, and so on and so forth.

1 Like

The Algorithm Projects are the kind of the final exam so expect that these will take some time. Many have spent days on just one of these projects.

If you want forum help you’re going to have to come with more than a blank challenge though. Broken is better than nothing. Even a series of steps written in plain language is a algorithm.

I will tell you there’s no obscure javascript needed to solve this - it is all logic. The whole thing could be solved with few while loops and if statements.

Below are some things to think about from the challenge and the hints to get you started. Since some campers don’t even want to peek at the hints I’m hiding the whole thing in an accordion.

Spoiling hints

From the challenge

You are given this information

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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}

Each of the function calls are very consistent in the way the cid is sent each time. How does that help you?

Not given but it is assumed that you know the value of each of these currencies.
If you are not from the U.S. you might need to look that up.

The 3 special cases

Return {status: "INSUFFICIENT_FUNDS", change: []}
#1 if cash-in-drawer is less than the change due, or
#2 if you cannot return the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change
#3 if it is equal to the change due.

Can you determine if any of these cases exist from the given information?
Would you need to calculate anything else?
At what point would you be able to determine if each of these cases existed?

The general case

Otherwise, return {status: "OPEN", change: [...]} , with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.

If you were a cashier how would you make the change?

From the hints

#1 It is easier when you know how much money is in your register beforehand. For this it is recommended to have a function to assign this information to a variable. Then you can see if you have enough money to complete the transaction and return change, or if you should close the register.

How does this relate to the challenge above?
How would you calculate this?
Have you done this yet?

#2 This problem is easier when you know the value of each bill or coin you are working with, rather than just the sum of each in the register. For example, it’s useful to know that a nickel is worth .05, along with the fact that you have $2.05 worth of nickels in the cash register.

How are you going to store that information? Look it up when needed?
What problems would this solve?
Have you attempted this yet?

#3 You will have to get as much change from one type of bill or coin before moving to the next, from greater to lesser value. Keep going until you have calculated all the change due.

This is an algorithm handed to you. You have to code that.

Can you state a more detailed algorithm in plain language? Pseudo code?

get as much change from one type of bill or coin before moving to the next

How will you know when to move on to the next?

Keep going until you have calculated all the change due.

How will you know when that is?

Good luck!

1 Like

Hey I’m working on that one too! The speaker in this vid is demonstrating broader programming concepts but uses a simple cash drawer as an example at 7:30. It’s not JavaScript but the code she presented helped me get started on my JS version.