Need an explanation on how to work my way through this challenge. :/

Im on the final challenge of the Javascript Basics module and wow, its hard.
I didnt quite understand the instructions. What is the aim? And what are we supposed to do with the cid? Are we supposed to convert it to dollars? If so then what is the use of it after that ? How do I find the change? etc…

Can someone give me a detailed but simple explanation of this?

Link to the challenge:

Yeah, this is a tough one - a lot of people get stopped up by this one. I remember that I struggled with it for a while.

Are the instructions confusing? Sometimes looking at the tests help. The second test says:

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]]}` .

So, it’s saying that if we call the function with:

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]])

That means a bill of 19.5 ($19.50) and they give 20 (presumable a $20 bill) and the cash register starts out with:

[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

then…

So that is the set up, you need to take that information, and do the calculations for what change you are going to give and return what change you are going to give them, in the form of:

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

In other words, you are going to give them $0.50 in quarters.

What is the aim?

Described above.

And what are we supposed to do with the cid?

That tells you how much money the store has in the cash register (cash in drawer) before the transaction.

Are we supposed to convert it [cid] to dollars? If so then what is the use of it after that ?

That is the issue. If you just convert it to a total dollar figure, how would you know if you have $10 in pennies or quarters?

How do I find the change?

How do humans do it? That is the point - to convert human thinking into a computer algorithm. If you were at the register, the bill was $19.50 and someone handed you a $20, what would be your thought process? Could you explain a simple process to a child? Maybe the child isn’t good at intuitive thinking but is very good at following simple instructions. Could you come up with a series of steps for them to do this?

I’m not trying to avoid your question, I’m trying to steer you in the right direction.

2 Likes

It’s getting easier once you took some time to think on how you would buy something with hard cash.
You have a price to pay, you give someone cash and then this someone has to take out money that is currently in the draw (cid) as appropriate change.

Keep in mind, if I buy something for 19.99$ but have a 100$ bill, I might not want want 8001 pennies back, unless there happens to be nothing but pennies in the drawer. And I don’t want 80$ back, but exactly 80.01$ → if that is not possible, look into the description.
Also you don’t add the cash to the drawer. Nope, the drawer only contains change so after I got the 80.01$ back, which just so happened to be exact money left in the drawer, then it’s empty. The 100$ I paid magically disappear. Which is not so bad because after I got the change, the drawer also disappears. The next function call get’s a new cid.

1 Like

@Jagaya @kevinSmith

Thanks for the help!
So I’ll put down what I understood so far.
Feel free to point out if im wrong.

The price is the cost of the product. The customer pays us cash . If the cash is more than the price, check our cid and return a change containing the amount to be payed as change to the customer .

1 Like

Cash will always be more than the price :wink:
But yeah, that’s the core idea.

1 Like

I have stumbled upon another doubt.
What would be the logic for finding which currency types to show up on the screen?
This is for when the case is OPEN
I was able to calculate the total, balance and stuff. Since the tests show that the tip returned should be an array with the correct currency, I’m not able to think of how to write the code needed to see what currency would be needed.
One idea I have is → check if the balance (cash - price) is lesser than or equal to a certain amount. Though it works for lower numbers I dont think it will work for higher numbers like

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 return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}

What should I do now? :confused:

If you had to do it manually, how would you give back the change?

You have to give back 96.74$

You can’t use the 100$ bill as it’s too high, but you can give back some twenties and tens, and so on with the smaller one.

How would you do it by hand?

2 Likes

What “certain amount”? You might be overthinking this, so here is a tip:
When giving out change in this task, you goal is to give the smallest amount of individual items back (meaning large bills first). If I get back 20$ and there is a 20$ bill, I want that bill - I don’t want two 10$ bills or twenty 1$ bills or any other combination. If it’s 19, I want TEN, FIVE, 4*ONE. If there is no TEN, then give me 3*FIVE, 4*ONE. There are only 2*FIVE? Then give me 2*FIVE and 9*ONE

Try to think what logic lies behind this way of giving back change.

1 Like

I somewhat get it now .

I think I would first look for a perfect value like if its a 0.5 dollar balance, then 2 quarters or if its a five dollar value , a $5 note or five $1 notes, etc
Otherwise, I would start looking through the drawer for other notes.

Thanks

1 Like

I should have elaborated more on this then, my bad.
The “Certain amount” I was talking about were the cases where I could directly get a perfect amount, by perfect amount I mean a single note of a certain currency . Like in the previous post I mentioned, $20-$19.5 = $0.5 to pay back so that would mean I would directly pay 2 quarters , etc…

Yep, will surely try that. Thanks!

1 Like

It sounds like you are headed in the right direction, but I didn’t hear the logic of the order of denominations to check.

I would start from biggest , ie, twenty in this case and go smaller.

1 Like

Right, that’s the idea.

But when you do that, you intuit it and know which denomination to start with. A computer can’t do that. It needs a methodically system, with no intuition. How do you know which to start with? Do you need to know or can you just always start in the same place?

Maybe you’re already there. If so, just ignore my ramblings.

2 Likes

Yep, thanks to the help I got, I was able to complete it.

Here’s the finished product -

I’ve put detailed comments so that someone can understand whats going on.

2 Likes