JavaScript Algorithms and Data Structures Projects - Cash Register

Hi ! I struggle coding the cash register. Here is my issue : I created an array cidCopy which contains the amount refunded, sorted by bills and coins. However, for some cids in imput my code is working well and for some others the calculations are wrong and cidCopy does not contain what it should.

For example :CheckCashRegister(99.99, 100, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]]) returns [ [ ‘PENNY’, 0.01 ], [ ‘NICKEL’, 0 ], [ ‘DIME’, 0 ], [ ‘QUARTER’, 0 ], [ ‘ONE’, 0 ], [ ‘FIVE’, 0 ], [ ‘TEN’, 0 ], [ ‘TWENTY’, 0 ], [ ‘ONE HUNDRED’, 0 ] ] as CidCopy, which is good.

However, CheckCashRegister(99.98, 100, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]]) still returns [ [ ‘PENNY’, 0.01 ], [ ‘NICKEL’, 0 ], [ ‘DIME’, 0 ], [ ‘QUARTER’, 0 ], [ ‘ONE’, 0 ], [ ‘FIVE’, 0 ], [ ‘TEN’, 0 ], [ ‘TWENTY’, 0 ], [ ‘ONE HUNDRED’, 0 ] ] as CidCopy.

function checkCashRegister(price, cash, cid) {

let change = cash - price;

const cidCopy = JSON.parse(JSON.stringify(cid));
for(let i = 0; i < cidCopy.length; i++) {
  cidCopy[i][1] = 0;
} // creates a copy of the cid array with all the quantities equal to zero


  let valueArr = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
  let countArr = [] 
  for(let i = 0; i < cid.length; i++) {
    countArr.push(cid[i][1]/valueArr[i]); //This array contains the number of each bill and coin we have
  }


// this for loop uses our bills and coins to make the change, starting with the highest usable bill. Each times it uses a bill or coin, it adds 1 to cidCopy so that it can count the number of items used
  for (let i = valueArr.length -1; i >= 0; i--) {

    while(change >= valueArr[i] && countArr[i] > 0) {
     change -= valueArr[i];
     countArr[i] -= 1;
     cidCopy[i][1]+=1;
    }
  }

  

for(let i = 0; i < cidCopy.length; i++) {
  cidCopy[i][1] = cidCopy[i][1]*valueArr[i];
} // multiplies each counting value of the cidCopy array with the value of the corresponding coins/bills 


}
checkCashRegister(99.98, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])

/*
  let remainingChangeCounter = 0;
// this for loop counts the bills and coins that are left in the register
  for(let count of countArr) {
   remainingChangeCounter += count;
  }

  function OpeningStatus() {
  this.status = "status";
  this.change = [];
 }

 let Insufficient = new OpeningStatus() 
 Insufficient.status = "INSUFFICIENT_FUNDS";
 Insufficient.change = [];
 
 let Closed = new OpeningStatus()
 Closed.status = "CLOSED";
 Closed.change = cidCopy;

 let Open = new OpeningStatus()
 Open.status = "OPEN";
 Open.change = cidCopy;*/

Your browser information:

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

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Start by writing the code so that it at least passes the first test and returns an object with the required parameters and work your way from there. Right now your function isn’t returning anything.

I suggest using the code that’s commented out or removing it altogether; there’s no point in including code that doesn’t do anything.

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