Cash Register - Javascript

Tell us what’s happening:
Describe your issue in detail here.

Hello,

I am trying to complete the last javascript exercise - Cash Register.

The last else statement cause only the first test to pass.

If I remove the last ‘else’ statement, the the last three tests pass.

Does anyone know why this is and how I can fix this problem

Thank you

   **Your code so far**
function checkCashRegister(price, cash, cid) {
let change;
 change = cash - price; 
for (var i = 0, l1 = cid.length; i < l1; i++) {
 
 for (var j = 0, l2 = cid[i].length; j < l2; j++) {
    
    if (cid[i][j] < change) { 
      const cool = {status: "INSUFFICIENT_FUNDS",change: []}
      return cool;
    } else if (cid[i][j] === change) {
      const myobj = {status: "CLOSED", change: cid}
      return myobj;
    } else {
       const car = {status: "OPEN", change: []}
       return car;
    } 
  }
}
}   


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/102.0.5005.124 Safari/537.36 Edg/102.0.1245.41

Challenge: Cash Register

Link to the challenge:

for (var i = 0, l1 = cid.length; i < l1; i++) {
  for (var j = 0, l2 = cid[i].length; j < l2; j++) {
    // here you check:
    // 1. if string 'PENNY' less than change
    // 2. if amount of money in penny less than change
    // 3. if string 'NICKEL' less than change
    // 4. if amount of money in nickel less than change
    // and so on
    // that is incorrect. you want to check if the sum of your money is less than change
    if (cid[i][j] < change) { 

Start with this one

What should be value of change?

How would I compare the word ‘pennh’ against integer number

I mean, change would contain an integer number

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

    for (var j = 0, l2 = cid[i].length; j < l2; j++) {
      console.log(`${cid[i][j]} < ${change}`, cid[i][j] < change)

Just add console.log() to see what your code compares.

Only the insufficent funds test pass
Only this test passes. {status: “INSUFFICIENT_FUNDS”, change: } The other don’t. What am I doing wrong?

function checkCashRegister(price, cash, cid) {
let change;
change = cash - price;
for (var i = 0, l1 = cid.length; i < l1; i++) {
for (var j = 0, l2 = cid[i].length; j < l2; j++) {
if (cid[i][j] < change) {
const car = {status: “INSUFFICIENT_FUNDS”, change: }
return car;
}
}
}

}

After this code:
function checkCashRegister(price, cash, cid) {
let change;
change = cash - price;
for (var i = 0, l1 = cid.length; i < l1; i++) {
for (var j = 0, l2 = cid[i].length; j < l2; j++) {
console.log(${cid[i][j]} < ${change}, cid[i][j] < change)
}
}
}
This is the output:
PENNY < 0.5 false
1.01 < 0.5 false
NICKEL < 0.5 false
2.05 < 0.5 false
DIME < 0.5 false
3.1 < 0.5 false
QUARTER < 0.5 false
4.25 < 0.5 false
ONE < 0.5 false
90 < 0.5 false
FIVE < 0.5 false
55 < 0.5 false
TEN < 0.5 false
20 < 0.5 false
TWENTY < 0.5 false
60 < 0.5 false
ONE HUNDRED < 0.5 false
100 < 0.5 false

I just dont get it, how can compare a string with a number. It wont work

I was just pointing out your mistakes. You don’t want to compare string and number.

This question is confusing. What condition would I need to test here

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.

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

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

if (cid[i][j] < change) { 
  const cool = {status: "INSUFFICIENT_FUNDS",change: []}
  return cool;
} else if (cid[i][j] === change) {
  const car = {status: "CLOSED",change:[["PENNY", 0.5], 
  ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], 
  ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], 
  ["ONE HUNDRED", 0]]}
  return car;
}

The task is reflecting real life situation. Suppose you work in a store and someone buys something and pays you $50 in cash, and you want to give them change, like $15. So you want to check your money to see if you can give them the change they need. If you have $100 in one bill, that means you have enough money, but you just can’t split it. Therefore, it would be “insufficient_funds.” If you only have $1, $5, and $5 bills, you can’t give change either. And the answer will be “insufficient_funds.” If you have exactly $15 (e.g. $1, $1, $1, $2, $10), then you just give them all your money and the status will be “closed”. If you have $5 and $5 bills and a $10 bill, then you give them one $5 and $10 bill, and you are left with $5, so the status will be “open”.

I get this part.
When you say sum of money, do you mean the total money in cash-in-drawer?

the change is 0.5. so basically I do not have 0.5 change in drawer

1 Like

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