Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Hello, I wrote the code for my Cash Register App project, and the automatic tests are telling me the output of what needs to be displayed in my changeDue div but if i manually run the tests changing the values of cid and price with the said cash the output is the same as asked in the test. I dont know what is wrong with my code since to me it seems that it-s working. I would appreciate if you could take a look at my code and tell me what is wrong with it.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang= "en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, scale=1.0">
    <title>cash register app</title>
  </head>
  <body>
    <h1>Cash register app</h1>
    <input id="cash">
    <button id="purchase-btn">Purchase</button>
    <div id="change-due"></div>
    <script src="script.js"></script>
  </body>
  </html>
  
/* file: styles.css */

/* file: script.js */
const input = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");


let price= 3.26;
let cash = 20;
let values = [
   ['PENNY', 0.01],
   ['NICKEL', 0.05],
   ['DIME', 0.1],
   ['QUARTER', 0.25],
   ['ONE', 1],
   ['FIVE', 5],
   ['TEN', 10],
   ['TWENTY', 20],
   ['ONE HUNDRED', 100]
 ];
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]];
var array= [];
var newArr;
var cidTot;
var modifiedCid = JSON.parse(JSON.stringify(cid))
var modifiedCidTot;

function checkCashRegister(price, cash, cid){
    
    var difference = cash - price;
    const originalDiff = difference;
    var objectReturn = {
        status: '',
        change: []
  };

  
  let n=0;
   for(let i=8; i>=0; i--){
    while (difference >= values[i][1] && modifiedCid[i][1] > 0){       //needfloatadjust
        
         if (n > 0 && array[n - 1][0] === values[i][0])  
         { array[n - 1][1] += values[i][1]; 
         array[n - 1][1] = parseFloat(array[n - 1][1].toFixed(2));}            //needfloatadjust
         else { 
          array.push([values[i][0], values[i][1]]);        //needfloatadjust
          n++;
          array[n - 1][1] = parseFloat(array[n - 1][1].toFixed(2));
         }   
         difference -=values[i][1];                           //needfloatadjust
         difference = Math.round(difference * 100) / 100;
         modifiedCid[i][1] -= values[i][1];                      //needfloatadjust
         modifiedCid[i][1] = parseFloat(modifiedCid[i][1].toFixed(2));
         
         console.log(difference); 
         
    }
    
};
cidTot = cid.map((element)=> element[1]).reduce((a,b)=>a+b);
modifiedCidTot = modifiedCid.map((element)=> element[1]).reduce((a,b)=>a+b);
newArr = array.map((element)=>element.join(": $")).join(" ");


if (cidTot < originalDiff || difference>0){
    objectReturn.status = "INSUFFICIENT_FUNDS";
    changeDue.textContent = "Status: "+Object.values(objectReturn).join(" ");
}
if (price<cash && cidTot === originalDiff){
    objectReturn.status = "CLOSED";
    objectReturn.change = newArr;
    changeDue.textContent = "Status: "+Object.values(objectReturn).join(" ");
}
if(price<cash && cidTot > originalDiff && difference === 0){
    objectReturn.status = "OPEN";
    objectReturn.change = newArr;
    changeDue.textContent = "Status: "+Object.values(objectReturn).join(" ");
}


}
purchaseBtn.addEventListener("click", ()=>{
    cash = input.value;
    if (!input.value){alert("Please insert a value");
  return;}
  if (cash<price){alert("Customer does not have enough money to purchase the item");
  return;}
  if(cash-price === 0){
   changeDue.textContent = "No change due - customer paid with exact cash";
   return;}
  checkCashRegister(price, cash, cid);
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

You should not make a bunch of global variables like this. It breaks the reusability of your function.

Also, you should not be using var.