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”.