JavaScript Cash Register: Using wrong Money values

I am currently making a cash register project for a the JavaScript projects but I am stumped on why my code isn’t working correctly. Basically I have a function takes three arguments one is the price of the product one is the amount of cash given to you and the other one is the cash currently in the drawer formatted in an array (See Below). What is supposed to happen is it returns the change as an array of the coins and values. There is no errors but the problem is it is using smaller values rather than the bigger ones as obviously the project requires the biggest coins to be used first. Here is what i mean

{ status: 'OPEN',
  change: 
   [ [ 'PENNY', 0.04 ],
     [ 'DIME', 0.2 ],
     [ 'QUARTER', 0.5 ],
     [ 'ONE', 16 ],
     [ 'FIVE', 30 ],
     [ 'TEN', 10 ],
     [ 'TWENTY', 40 ] ] }

I don’t want it to do that instead i want it to use 3 twenties to make 60 and two tens instead of extra fives and ones as there is enough to do so. I think the problem is in this bit of code:

let cidRev = Array.prototype.slice.call(coinsID).reverse();
    let curRev = Array.prototype.slice.call(currency).reverse();
    let curyRev = Array.prototype.slice.call(curryency).reverse();
    for (let i = 0; i < cidRev.length; i++){
      for (let j = 0; j < cidRev[i]; j++){
        changeOwed = Math.round((changeOwed) * 100) / 100
        if (changeOwed - curRev[i] >= 0){
          changeOwed -= curRev[i]
          cidRev[i] -= 1
          changeTemp.push(curyRev[i])
        }
      }
    }

But here is my code:

function checkCashRegister(price, cash, cid) {
  
  let change = [];
  let changeTemp = [];
  let status = "";
  let coinsID = [];
  let totalCID = 0;
  let curryency = ["PENNY", "NICKEL", "DIME", "QUARTER", "ONE", "FIVE", "TEN", "TWENTY", "ONE HUNDRED"]
  let currency = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
  let changeOwed = cash - price


  for (let i = 0; i < cid.length; i++){
    coinsID[i] = Math.round(cid[i][1] / currency[i])
    totalCID = Math.round((totalCID + cid[i][1]) * 100) / 100;
  }

  if (totalCID == changeOwed){
    status = "CLOSED";
    change = cid;
  }
  else if (totalCID < changeOwed){
    status = "INSUFFICIENT_FUNDS"
    change = []
  }
  else {
    let cidRev = Array.prototype.slice.call(coinsID).reverse();
    let curRev = Array.prototype.slice.call(currency).reverse();
    let curyRev = Array.prototype.slice.call(curryency).reverse();
    for (let i = 0; i < cidRev.length; i++){
      for (let j = 0; j < cidRev[i]; j++){
        changeOwed = Math.round((changeOwed) * 100) / 100
        if (changeOwed - curRev[i] >= 0){
          changeOwed -= curRev[i]
          cidRev[i] -= 1
          changeTemp.push(curyRev[i])
        }
      }
    }
    for (let i = 0; i < curryency.length; i++){
      change.push([curryency[i], (changeTemp.filter(x => x === curryency[i]).length) * currency[i]]);
    }
    change = change.filter(x => x[1] !== 0)


    status = "OPEN"
    console.log(coinsID, cidRev)
    console.log(curRev)
    console.log(changeOwed)
    if (changeOwed > 0){
      status = "INSUFFICIENT_FUNDS"
      change = []
    }
  }
  

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

Thanks for any help in advance!

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