Cash Register - Javascript

function checkCashRegister(price, cash, cid) {

let change;

change = cash - price;
console.log(change);

for (var i = 0, l1 = cid.length; i < l1; i++) {

for (var j = 0; j < cid[i].length; j++) {

 if (1.01 < change) {
      return {status: "INSUFFICIENT_FUNDS", change: []}
 }
 if (1.01 < change) {
      return {status: "INSUFFICIENT_FUNDS", change: []}
 }

}
}
}

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

it has been a while since you posted about this so please update us on what you currently need help with (what part of this challenge is still confusing to you)

I just do not fully undetstand this whole challene of cash register.
why do you have different currencies. Could please tell me on which part of the code I am making a mistak and why

okay then try to just focus on understanding the problem first before trying to write or fix any code.

First, there is only one currency. The $ dollar currency.
Are you familiar with American dollars and the denominations? (penny, nickel, dime etc?)

If you have any question about this let me know.

Beyond that, you need to examine the description and test cases of this challenge to get a handle on what is needed. You can do it in pieces, you don’t have to understand everything at once.

For eg. you understand that the checkCashRegister function accepts a price, a payment amount, and an array as the 3rd parameter.
Do you understand each of these? IF you have any concern about any of the inputs, let me know.

Then after understanding this, move forward to the expected return values.
What are the possibilities, which one returned when?

Try to summarize them and we can look your summary over to make sure you have the correct understanding.
I’ll get you started:

The checkCashRegister calculates how much money is being returned to the customer when they purchase something. It does this by returning an object containing the status string of the register and the change array. The status string can be one of OPEN, CLOSED or INSUFFICIENT_FUNDS. The change array will return different things depending on the status string.

It will return an empty array if the cash register doesn’t have the funds to give back any change to customer.
It will return a <fill this in and continue>

No - I am Not familiar with the American dollars and the denominations? (penny, nickel, dime etc?)

Yes - I understand that the checkCashRegister function accepts a price, a payment amount, and an array as the 3rd parameter. I understand the javascript syntax

For the American money, here is what you need to know:

  • One-cent coin: Penny
  • Five-cent coin: Nickel
  • 10-cent coin: Dime
  • 25-cent coin: Quarter

And of course there is the one dollar bill (one dollar is equivalent to 100 cents) and the rest of the bigger values are paper bills.

ok.
I have don this subtraction:
cash - price = 0.5
so what do I do with 0.5?

I have use an if statement inside an inner for loop: Here is my code. Only “Insufficient fund”, test pass. I just dont understand.

function checkCashRegister(price, cash, cid) {

let change;

change = cash - price;
console.log(change);

for (var i = 0, l1 = cid.length; i < l1; i++) {

for (var j = 0; j < cid[i].length; j++) {

 if (cid[i][j] != change) {
      return {status: "INSUFFICIENT_FUNDS", change: []}
 }

 if (cid[i][j] == change) {
     return {status: "CLOSED", change: []}
 }

}
}
}

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

how can I give 0.5 in change. I dont think I can.

let change;

change = cash - price;
console.log(change);

I am getting this output:
0.5
0.5
0.5
96.74
0.5
0.5
0.5
0.5

try to think about what you would do if you had a real cash register in front of you.
And you need to give back 0.5 of a dollar (which is 50 cents).

You would check you register to see what denominations of coins you have.
For eg. if you have 0 pennies, 0 nickels, 0 dimes and 0 quarters, then you cannot give the customer back any change because you don’t have small enough denominations.

But if you have 50 pennies, you can give 50 pennies back.
Or if you have 2 quarters, then you can give 2 quarters back.
Or if you have 1 quarter and 2 dimes and 1 nickel, then that works also.

  1. what does [“PENNY”, 1.01] mean?
  2. what does [“NICKEL”, 2.05] mean?
  3. what does [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], mean?

I just dont understand these.

these amounts represent the total amount in $ of money in the given denomination.
So for:
[“PENNY”, 1.01]
It means there are 101 pennies in the drawer

[“NICKEL”, 2.05]
means there are 41 nickels (41 x 5 = 205 cents)

[“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20],
This one means the drawer has 17 quarters, 90 dollar bills, 11 five dollar bills and 2 ten dollar bills.

This task is just too advanced for beginners. I won’t be able to complete it.
I was really looking forward to completing the javascript Data Structures but its just too much and too complicated. We are still beginners not advanced coders.

I give up

Thank you for trying to help

I am sorry to hear that.
If there is anything else I can help you understand, please do not hesitate to ask.

This task is not for beginners. It is for people who have completed the entire freeCodeCamp basic JavaScript course and have intermediate skills. I recommend reviewing that course without copying or looking up answers if you cannot complete this project.

I havent copied anyones code

I am talking about the previous challenges. I’m not saying that you did copy necessarily, but usually when someone gets to the projects and finds them impossible, it is because they didn’t build problem solving skills during the curriculum. The most common cause of that is that they looked up solutions to the previous challenges before they finished writing their own.

In any case, practicing problem solving without looking up answers in a good idea if you find this totally impossible.

A post was split to a new topic: Cash register help

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