Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Do not pass certain test case even though the output is exactly the same as wanted by the test case. This is one of the test case that I have problem with:
When price is 19.5 , the value in the #cash element is 20 , cid is [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]] , and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: INSUFFICIENT_FUNDS". My code so far is as below:

Your code so far

let price = 3.26;
const cid = [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90],
  ['FIVE', 55],
  ['TEN', 20],
  ['TWENTY', 60],
  ['ONE HUNDRED', 100]
];

const cashOut = [
  ['PENNY', 0.00],
  ['NICKEL', 0.00],
  ['DIME', 0.00],
  ['QUARTER', 0.00],
  ['ONE', 0],
  ['FIVE', 0],
  ['TEN', 0],
  ['TWENTY', 0],
  ['ONE HUNDRED', 0]
];

let cash = document.getElementById("cash");
let total = document.getElementById("totalm");
let cashIn = [...document.querySelectorAll("li")];
let purchaseButton = document.getElementById("purchase-btn");
let changeDue = document.getElementById("change-due");
let pennies = document.getElementById("pennies");
let nickles = document.getElementById("nickles");
let dimes = document.getElementById("dimes");
let quarters = document.getElementById("quarters");
let dollar = document.getElementById("dollar");
let five = document.getElementById("five");
let ten = document.getElementById("ten");
let twenty = document.getElementById("twenty");
let hundred = document.getElementById("hundred");

const changeChecker = (changeRequired) => {
  
  let conditionBreak = true;
  let status;
  while(changeRequired !== 0 && conditionBreak) {
    switch(true) {
      case(cid[8][1] > 0.00 && changeRequired >= 100):
        changeRequired = (changeRequired - 100).toFixed(2);
        cid[8][1] = (cid[8][1] - 100).toFixed(2);
        cashOut[8][1] = parseFloat((cashOut[8][1] + 100).toFixed(2));
        break;
      case(cid[7][1] > 0.00 && changeRequired >= 20):
        changeRequired = (changeRequired - 20).toFixed(2);
        cid[7][1] = (cid[7][1] - 20).toFixed(2);
        cashOut[7][1] = parseFloat((cashOut[7][1] + 20).toFixed(2));
        break;
      case(cid[6][1] > 0.00 && changeRequired >= 10):
        changeRequired = (changeRequired - 10).toFixed(2);
        cid[6][1] = (cid[6][1] - 10).toFixed(2);
        cashOut[6][1] = parseFloat((cashOut[6][1] + 10).toFixed(2));
        break;
      case(cid[5][1] > 0.00 && changeRequired >= 5):
        changeRequired = (changeRequired - 5).toFixed(2);
        cid[5][1] = (cid[5][1] - 5).toFixed(2);
        cashOut[5][1] = parseFloat((cashOut[5][1] + 5).toFixed(2));
        break;
      case(cid[4][1] > 0.00 && changeRequired >= 1):
        changeRequired = (changeRequired - 1).toFixed(2);
        cid[4][1] = (cid[4][1] - 1).toFixed(2);
        cashOut[4][1] = parseFloat((cashOut[4][1] + 1).toFixed(2));
        break;
      case(cid[3][1] > 0.00 && changeRequired >= 0.25):
        changeRequired = (changeRequired - 0.25).toFixed(2);
        cid[3][1] = (cid[3][1] - 0.25).toFixed(2);
        cashOut[3][1] = parseFloat((cashOut[3][1] + 0.25).toFixed(2));
        break;
      case(cid[2][1] > 0.00 && changeRequired >= 0.1):
        changeRequired = (changeRequired - 0.1).toFixed(2);
        cid[2][1] = (cid[2][1] - 0.1).toFixed(2);
        cashOut[2][1] = parseFloat((cashOut[2][1] + 0.1).toFixed(2));
        break;
      case(cid[1][1] > 0.00 && changeRequired >= 0.05):
        changeRequired = (changeRequired - 0.05).toFixed(2);
        cid[1][1] = (cid[1][1] - 0.5).toFixed(2);
        cashOut[1][1] = parseFloat((cashOut[1][1] + 0.05).toFixed(2));
        break;
      case(cid[0][1] > 0.00 && changeRequired >= 0.01):
        changeRequired = (changeRequired - 0.01).toFixed(2);
        cid[0][1] = (cid[0][1] - 0.01).toFixed(2);
        cashOut[0][1] = parseFloat((cashOut[0][1] + 0.01).toFixed(2));
        break;
      default:
        conditionBreak = !conditionBreak;
        if (changeRequired > 0) {
          status = "INSUFFICIENT_FUNDS";
          changeDue.textContent += `Status: ${status}`;
        } else if (cid.filter((elem) => elem[1] > 0).length == 0) {
          status = "CLOSED";
          let tempArr = cashOut.filter(elem => elem[1] > 0).reverse();
          changeDue.textContent += `Status: ${status} \n`
          for (const elem of tempArr) {
            changeDue.textContent += `${elem[0]}: \$${elem[1]} `
          }
        } else {
          status = "OPEN";
          let tempArr = [...cashOut.filter(elem => elem[1] > 0)].reverse();
          changeDue.textContent += `Status: ${status} \n`
          for (const elem of tempArr) {
            changeDue.textContent += `${elem[0]}: \$${elem[1]} `
          }
          console.log(changeDue.textContent);
        }
        break;
    }    

  }
  update();
}

purchaseButton.addEventListener("click", () => {
  changeDue.textContent = "";
  let cashAmount = cash.value;
  if (cashAmount < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cashAmount == price) {
    changeDue.textContent = "No change due - customer paid with exact cash";
  } else {
    console.log('hai')
    const changeRequired = cashAmount - price;
    changeChecker(changeRequired);
  }
})

const update = () => {
  pennies.textContent = cid[0][1];
  nickles.textContent = cid[1][1];
  dimes.textContent = cid[2][1];
  quarters.textContent = cid[3][1];
  dollar.textContent = cid[4][1];
  five.textContent = cid[5][1];
  ten.textContent = cid[6][1];
  twenty.textContent = cid[7][1];
  hundred.textContent = cid[8][1];
  total.textContent = ` ${ price}`;
}

update();
<!-- file: index.html -->

/* file: script.js */

/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

This global variable means your function can only be used once’ but the tests call your function many times in a row