Why is this not passing third test?

Tell us what’s happening:
This seems like it should work for this test case…there is enough cid to make exact change for this transaction, but it is not working for whatever reason.

  **Your code so far**

const currencyUnit = {
"PENNY": 1,
"NICKEL": 5,
"DIME": 10,
"QUARTER": 25,
"DOLLAR": 100,
"FIVE DOLLARS": 500,
"TEN DOLLARS": 1000,
"TWENTY DOLLARS": 2000,
"ONE HUNDRED DOLLARS": 10000
};  


function checkCashRegister(price, cash, cid) {

let changeSum = cash * 100  - price * 100;
let changeSumCheck = changeSum;
let change = [];
let status = '';

let cidSum = 0;
let filteredCid = cid.filter(elem => elem[1] !== 0).reverse();

filteredCid.forEach(elem => {
  let curr = elem[0];
  let currSum = elem[1] * 100;
  cidSum += currSum;
  let amount = 0;
  while(changeSum >= currencyUnit[curr] && currSum > 0) {
    amount += currencyUnit[curr];
    changeSum -= currencyUnit[curr];
    currSum -= currencyUnit[curr];
  }
  if(amount !== 0){
    change.push([curr, amount/100]);
  }
});

if(changeSum > 0){
  status = 'INSUFFICIENT_FUNDS';
  change = [];
} else if(changeSum == 0 && changeSumCheck == cidSum){
  status = 'CLOSED';
  change = cid;
} else {status = 'OPEN'};

return {'status': status, 'change': change};





}  

console.log(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]]))
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13597.105.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.208 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

I don’t have time to go through the whole thing, but one clear mistake is in your currencyUnit object - the object properties for the dollar amounts don’t match what the test is using. If you fix that, you get it sending back the right amount, but it is just not normalized correctly. So for that test, you are getting:

{
  "status": "OPEN",
  "change": [
    [
      "TWENTY",
      60
    ],
    [
      "TEN",
      20
    ],
    [
      "FIVE",
      15
    ],
    [
      "QUARTER",
      1.5
    ],
    [
      "DIME",
      0.2
    ],
    [
      "PENNY",
      0.04
    ]
  ]
}

But you should be getting:

{
  "status": "OPEN",
  "change": [
    [
      "TWENTY",
      60
    ],
    [
      "TEN",
      20
    ],
    [
      "FIVE",
      15
    ],
    [
      "ONE",
      1
    ],
    [
      "QUARTER",
      0.5
    ],
    [
      "DIME",
      0.2
    ],
    [
      "PENNY",
      0.04
    ]
  ]
}

Do you see the difference?

1 Like

i got it now thank you. i just had to remove the word ‘dollars’ from the currencyUnit object.

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