Projects: Cash Register

Hello,
I am a single project away from getting the second certificate. Can anyone help me how can I go on from here with my code?

function checkCashRegister(price, cash, cid) {
  let moneyInfo = [
  { type: "ONE HUNDRED", value: 100.0 },
  { type: "TWENTY", value: 20.0 },
  { type: "TEN", value: 10.0 },
  { type: "FIVE", value: 5.0 },
  { type: "ONE", value: 1.0 },
  { type: "QUARTER", value: 0.25 },
  { type: "DIME", value: 0.1 },
  { type: "NICKEL", value: 0.05 },
  { type: "PENNY", value: 0.01 }
  ];
  let moneyTypes=["ONE HUNDRED","TWENTY","TEN","FIVE","ONE","QUARTER","DIME","NICKEL","PENNY"]
  let moneyValues=[100.0,20.0,10.0,1.0,0.25,0.1,0.05,0.01]
  //how much we owe after the transaction
  let changeDue=cash-price;
  changeDue=Math.round(changeDue * 100) / 100;
  let ourRegister=[...cid];
  //ourRegister is copy of cid with totalValueOfCid added as total


  // total value of cash in cid
  let totalValueOfCid=0;
  let cidArr=Object.entries(cid).flat(99)
  for(let i=2;i<cidArr.length;i=i+3){ 
    totalValueOfCid+=cidArr[i];
  }

  totalValueOfCid = Math.round(totalValueOfCid * 100) / 100;
  ourRegister.total=totalValueOfCid;
  console.log(ourRegister)



 

  let response={status: null, change: []};
  if(changeDue>ourRegister.total){
    response.status="INSUFFICIENT_FUNDS";
    return response;
  }

  if(changeDue===ourRegister.total){
    response.status="CLOSED";
    response.change=cid;
    return response;
  }

let answer= "";
  for (let i = 0; i < moneyValues.length; i++) {
    while (moneyValues[i] <= changeDue) {
      changeDue = changeDue-moneyValues[i];
      answer = answer + moneyTypes[i];
    }
  }

  




  response.status = "OPEN";
  response.change = answer;
  return response;




  
}



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


So

Hi @ofk8vb! and welcome to freeCodeCamp. First of all, congrats on making it as far as you have!

I see that you are iterating through the denominations, and seeing if the current change required is equal to or greater than that.

I also see that to determine whether you have sufficient funds or not, you are looking at the total value of what is in the cash register, and seeing if that is greater than the change that is due.

First off, I like your line of thinking, and you are very much on the right path. But remember, for both of these points, you also have to take into consideration the number of instances of each denomination that you have in your cash register. So I would find a way to track what is in your cash register, and reduce from that and add to your change that you are giving to the customer.

Also, I would avoid having decimals in your code, especially at the beginning, as it will create inaccuracies in the numbers due to floating-point precision issues. For example:

0.2 + 0.4 = 0.6000000000000001

Also, looking through your code, I see that to access the values in the cash register you flattened the array and accessed every third one. You could make this a little simpler and use Array.prototype.map().

let cidValues = cid.map(val => Math.round(val[1]*100)/100);

In Summary:
:slightly_smiling_face: You are on the right track, just make sure to track and take from the cash register instead of just what is numerically possible. Also, try and avoid decimals.

1 Like

I tried to offer broader guidance instead of actual solutions, so if you continue to have difficulty, please let me know :slight_smile: