Why is my code for the Cash Register algorithm subtracting wrong!

function checkCashRegister(price, cash, cid) {
  let y = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20.0],
  ["ONE HUNDRED", 100]
];
 let r = 0 ;

  let x = cash-price
  console.log(x,'x')
  let ct = cid[0][1]+cid[1][1]+cid[2][1] +cid[3][1]+cid[4][1]+cid[5][1]+cid[6][1]+cid[7][1]+cid[8][1];

  let change = [];     
for(let i = cid.length-1;i>=0;i--){
  let p = cid[i][1]/y[i][1];
    while((p > 0)&(x-y[i][1]>=0)){
       
       x = x-y[i][1]
       
        r += y[i][1]
        console.log(r,'r',i,'i',x)
       p--;
     } 
     change.push([y[i][0],r])
     r =0;
     console.log(change)
    }
 }

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

You should not use any global variables that you mutate as the function executes.

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