Cash Register test 7, output correct but not passing test

All tests pass except for 7, while it was working previously when other tests did not work now it does not pass despite my code producing the output. Would be great if someone could point me in the right direction!

const cash = document.getElementById("cash");
const priceField = document.getElementById("price-field");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDueDiv = document.getElementById("change-due");
const clearBtn = document.getElementById("clear-btn");

let price = 3.26;
let cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];
// let cid = [
//   ["PENNY", 0.01],
//   ["NICKEL", 0],
//   ["DIME", 0 ],
//   ["QUARTER", 0],
//   ["ONE", 1],
//   ["FIVE", 0],
//   ["TEN", 0],
//   ["TWENTY", 0],
//   ["ONE HUNDRED", 0]
// ];

const currencyArr = [1,5,10,25,100,500,1000,2000,10000];

let resultArr = [];
let changeBackArr = [];

priceField.innerText = `Price: ${price}`

// changeDue function
const changeDue = (cash, price) => {
  let change = 0;
  price = (price * 100);
  cash = (cash.value * 100);
  change = ((cash - price)/100);  
  return change; }; 

// My initial approach to cidSum
const cidSum = (cid) => { 
  let sum = 0;
  for(let i=0; i<cid.length; i++){
     sum += (cid[i][1]*100);
  } 
sum = sum/100;
return sum; };


// // Update cid function
const updateCid = (cash, price) => { 
  let count = 0;
  let change = (changeDue(cash, price))*(100);
  cid.forEach((element)=>{
    element[1] *= 100; // multiplies $ values by 100
    element[1] = Math.ceil(element[1]); // rounds up
  })

for(let i=8; i>=0; i--){  
while(i>=0 && change >= currencyArr[i] && change > 0 && cid[i][1] >= currencyArr[i]){
      cid[i][1] -= currencyArr[i];
      change -= currencyArr[i];
      count++;  
    if(count > 0){
      resultArr.push([cid[i][0], currencyArr[i]]);
    }  
  }
} 
 changeBackArr  = resultArr.reduce((acc, [label, value]) => {
    const existingItem = acc.find(item => item[0] === label);
    if (existingItem) { 
        existingItem[1] += value;
    } else {
        acc.push([label, value]);
    }
    return acc;
}, []);
};

// // Register Status Function 
const regStatus = (cash,price,cid) =>{
  let sum = (cidSum(cid));
  updateCid(cash,price); 
  let change = changeDue(cash, price); 

 let changeBackSum = 0;

  // changeBackArrSum
const changeBackArrSum = (changeBackArr) => { 
  for(let i=0; i<changeBackArr.length; i++){
    changeBackSum += changeBackArr[i][1];
  } changeBackSum = ((changeBackSum)/(100));
    return changeBackSum; 
  };
 changeBackArrSum(changeBackArr);
  
  cash = (cash.value * 100);
  price = (price * 100);
  // console.log(changeBackArr);
  // console.log('cash',cash);
  // console.log('price',price);
  // console.log('changeBackSum',changeBackSum);
  // console.log('sum',sum);
  // console.log('change',change);
  if(cash < price){ 
    alert("Customer does not have enough money to purchase the item");
  } else if(cash === price){
  changeDueDiv.textContent = `No change due - customer paid with exact cash`
  } else if(changeBackSum < change || sum < change ){ 
  changeDueDiv.textContent = `Status: INSUFFICIENT_FUNDS`;
  } else if(sum === change || (sum-change <= 0)){
  changeDueDiv.textContent = `Status: CLOSED`;
  for(let i=0; i<changeBackArr.length; i++){
      changeDueDiv.textContent += ` ${changeBackArr[i][0]}: $${((changeBackArr[i][1])/100)}`;
    }
  } else if(sum > change && changeBackSum === change){
    changeDueDiv.textContent = `Status: OPEN`;
    for(let i=0; i<changeBackArr.length; i++){
      changeDueDiv.textContent += ` ${changeBackArr[i][0]}: $${((changeBackArr[i][1])/100)}`;
    }} 
}

// Purchase Button + changeDue Function
purchaseBtn.addEventListener("click", () => {
  // add every function here so it is called with "click" 
  regStatus(cash, price, cid);
});

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cash Register</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>Cash Register Project</h1>
    <h3 id="price-field"></h3>
    <div class="input-container">

      <label>Enter cash from customer:</label>
      <input id="cash" type="number" />
      <button id="purchase-btn">Purchase</button>
      <button id="clear-btn">Clear</button>
 
    </div>
    <div id="change-due">
    <output id="result" for="number-input"></output>
    </div>
    
    
    <script src="script.js"></script>
  </body>
</html>

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like