JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
The last test case seems to be wrong. The problem statement states that the change due in coins and bills should be sorted in highest to lowest order, as the value of the change key. Also, the denominations which are not used are included in the test case.

Here’s the test case:

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]} .

  **Your code so far**
let currency = [
{ name: "ONE HUNDRED", val: 100 },
{ name: "TWENTY", val: 20 },
{ name: "TEN", val: 10 },
{ name: "FIVE", val: 5 },
{ name: "ONE", val: 1 },
{ name: "QUARTER", val: 0.25 },
{ name: "DIME", val: 0.1 },
{ name: "NICKEL", val: 0.05 },
{ name: "PENNY", val: 0.01 }
];

function resolveFraction(num) {
return parseFloat(num.toFixed(2));
}

function checkCashRegister(price, cash, cid) {
let changeStatus = { status: "OPEN", change: [] };
let returnAmount = resolveFraction(cash - price);
let index = 0;
let roundAmount = 0;
let directory = cid.reduce((acc, cur) => {
  acc[cur[0]] = cur[1];
  return acc;
}, {});

index = currency.findIndex(elem => elem.val <= returnAmount);

while (returnAmount >= 0) {
  if(index >= currency.length) {
    break;
  }

  roundAmount = Math.floor(returnAmount/currency[index].val) * currency[index].val;

  if(roundAmount === 0 || directory[currency[index].name] === 0) {
    index++;
    continue;
  }
  
  if(directory[currency[index].name] <= roundAmount) {
    changeStatus.change.push([currency[index].name, directory[currency[index].name]]);
    returnAmount = resolveFraction(returnAmount - directory[currency[index].name]);
    directory[currency[index].name] = 0;
    index++;
    continue;
  }

  changeStatus.change.push([currency[index].name, roundAmount]);
    returnAmount = resolveFraction(returnAmount - roundAmount);
    directory[currency[index].name] = resolveFraction(directory[currency[index].name] - roundAmount);
    index++;
}

if(returnAmount != 0) {
  return {status: "INSUFFICIENT_FUNDS", change: []};
}

if(Object.values(directory).reduce((acc,cur) => acc + cur, 0) === 0) {
  changeStatus.status = "CLOSED";
};

return changeStatus;
}

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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

that last test case is status CLOSED, not OPEN, it’s correct like this

1 Like

What about the order of the output in change?

did you read the CLOSED condition?

Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.

Sorry, my bad. I misinterpreted that.

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