Build a Cash Register Project - Build a Cash Register

Hi everyone :wave:! I wrote correct code. My answers and answers from the tests are the same, but test fail my code. I don’t know where is an error. What can I do in this situation?
Thank you in advance for your replies! :smiley:

Here is my code:

<input id="price">
<input id="cash">
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>

<script>

  function checkCashRegister(price, cash, cid) {
  const currencyUnit = {
    'PENNY': 0.01,
    'NICKEL': 0.05,
    'DIME': 0.1,
    'QUARTER': 0.25,
    'ONE': 1,
    'FIVE': 5,
    'TEN': 10,
    'TWENTY': 20,
    'ONE HUNDRED': 100,
  };

  let changeDue = cash - price;
  let change = [];
  let totalCID = 0;

  for (let i = 0; i < cid.length; i++) {
    totalCID += cid[i][1];
  }
  totalCID = parseFloat(totalCID.toFixed(2));

  if (changeDue > totalCID) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  if (changeDue === totalCID) {
    return { status: "CLOSED", change: cid };
  }

  for (let i = cid.length - 1; i >= 0; i--) {
    const unitValue = currencyUnit[cid[i][0]];
    const maxUnits = Math.floor(cid[i][1] / unitValue);
    const returnedUnits = Math.min(maxUnits, Math.floor(changeDue / unitValue));

    if (returnedUnits > 0) {
      change.push([cid[i][0], returnedUnits * unitValue]);
      changeDue = parseFloat((changeDue - returnedUnits * unitValue).toFixed(2));
    }
  }

  if (changeDue > 0) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  return { status: "OPEN", change: change };
}

const price= document.getElementById("price");
const cash= document.getElementById("cash");
const changeDue = document.getElementById("change-due")

const button = document.getElementById("purchase-btn");
button.addEventListener("click", ()=>{
  const cashValue = parseFloat(cash.value);
  const priceValue = parseFloat(price.value);
  
  if(cashValue < priceValue){
    alert("Customer does not have enough money to purchase the item")
  }else if(cashValue == priceValue){
    changeDue.innerHTML = "No change due - customer paid with exact cash"
  }else{
    const data = checkCashRegister(parseFloat(price.value), parseFloat(cash.value), [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
    let changeString = '';
    for(let i = 0; i < data.change.length; i++){
      if(data.change[i][1] > 0){
        changeString += `${data.change[i][0]}: $${data.change[i][1]} `;
      }
    }
    changeDue.innerHTML =`Status:  ${data.status} ${changeString}`
  }
})
</script>

Challenge Information:

Build a Cash Register Project - Build a Cash Register

You shouldn’t use any additional global variables. Global variables spoil the ability to use your function multiple times if you aren’t careful

Now I found the solution. Moment ago I saw that I need to add .js file to html file and data like cid or price are in script.js file. :saluting_face:

Thank you for your suggestion buddy :grinning: