Cash Register (Final problem to JS)

Tell us what’s happening:
On 5 of the 6 test cases, I pass. My result only includes the change necessary to display (if there are 0 of a denomination, I don’t add it to the result). However, the final test case, I fail for some reason.

I indicate that the change is 5 pennies, and that the status of the cash register is closed, yet the case still fails.
Your code so far


function checkCashRegister(price, cash, cid) {

let change = cash - price;
let denomIndex = 0;

let result = {status: "", change: []};
let values = [.01, .05, .10, .25, 1, 5, 10, 20, 100];
for (let i = 8; i >= 0; i--) {
  // case: check if we can give change for this value (change exceeds the value)
  if (!(change >= values[i] && cid[i][1] > 0)) continue;
  
  result.change[denomIndex] = [cid[i][0], 0];
  
  while (change >= values[i] && cid[i][1] > 0) {

    
    // console.log(change, values[i], cid[i][1]);
    change = Math.round((change - values[i])*100)/100;
    cid[i][1] = Math.round((cid[i][1] - values[i])*100)/100;
    
    // change -= values[i];

    // create the entry in change
    
    result.change[denomIndex][1] = Math.round((result.change[denomIndex][1] + values[i])*100)/100; 
    // console.log(result.change)
  }
  denomIndex++;
}
if (change > 0) return {status: "INSUFFICIENT_FUNDS", change: []};

let remaining = false;
for (const denom of cid) {
  if (denom[1]) remaining = true;
  break;
}
result.status = (remaining) ? "OPEN" : "CLOSED";

console.log(result);
return result; 
}

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

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

// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

It’s asking to return cid if it contains the exact change due

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

1 Like

You have to return the full array. You code returns

{ status: 'CLOSED', change: [ [ 'PENNY', 0.5 ] ] }

But, as shown in the test case scenario, it 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]]}```
1 Like

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