I'm stuck is the Cash Register problem

So when i console.log my code it is right, buw the web doesnt accept it. The results are numbers, not strings, so i don’t know what is the issue. Thanks!

const value = {
    1: 10000,
    2: 2000,
    3: 1000,
    4: 500,
    5: 100,
    6: 25,
    7: 10,
    8: 5,
    9: 1
    
  };
  
  const newCid = [
    [ 'ONE HUNDRED', 0 ],
    [ 'TWENTY', 0 ],
    [ 'TEN', 0 ],
    [ 'FIVE', 0 ],
    [ 'ONE', 0 ],
    [ 'QUARTER', 0 ],
    [ 'DIME', 0 ],
    [ 'NICKEL', 0 ],
    [ 'PENNY', 0 ]
  ];
  
  function checkCashRegister(price, cash, cid) {
    let change = cash * 100 - price * 100;
    let cashDrawer = cid.map( x => x[1] * 100).reverse();

    for (let i = 0; i < cashDrawer.length; i++) {
      while (change >= value[i + 1] && cashDrawer[i] > 0) {
        change -= value[i + 1];
        cashDrawer[i] -= value[i + 1];
        newCid[i][1] += value[i + 1];
      }
    }
    
    let drawerStatus = false;
    for (let i = 0; i < cashDrawer.length; i++) {
      if (cashDrawer[i] > 0) {
        drawerStatus = true;
      }
    }
    const cid1 = newCid.map(x => [x[0], x[1]/ 100]);
    const cid2 = newCid.map(x => [x[0], x[1]/ 100]).reverse();

    for (let i = 0; i < cid1 .length; i++) {
      if (cid1 [i][1] == 0) {
        cid1 .splice(i, 1);
        i -= 1;
      }
    }
    

    if (change > 0) {
      return {status: "INSUFFICIENT_FUNDS", change: []}
    } else if (change == 0 && drawerStatus != true) {
      return { status: "CLOSED", change: cid2 }
    } else if (change == 0 && drawerStatus == true) {
      return { status: "OPEN", change: cid1 }
    }
  }
  
  
  
  console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

Using global variables breaks the reusability of your function here.

2 Likes

If you remove the function call at the bottom it will pass.

But you should really scope the top-level array to the function instead so it gets redeclared/reset between runs.

1 Like

Thank you so much, it worked!!

2 Likes

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