Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I have coded out my solution for the cash register projects, although it quite over engineered and maybe not the best solution, it works or should I say it should work. when I test all the test cases and user story manually it works as expected but in the FCC test am failing some test and when I try changing he variable CID (cash in drawer)and the the price then click for the result the correct answer is generated and on running the test again the specific test passes

Your code so far

<!-- file: index.html -->
 <!DOCTYPE html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <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>
    <div class="ui">
        <div id="change-due">
            
        </div>
        <div id="cash-container">
            <input type="number" id="cash" placeholder="input cash">
        </div>
        <button id="purchase-btn">Purchase</button>
        <div id="drawer"> 
          <h2>Change In Drawer</h2>
        </div>
    </div>
      <script src="script.js"></script>
 </body>
 </html>
/* file: styles.css */

/* file: script.js */
 const purchaseBtn=document.getElementById('purchase-btn');
const change_div=document.getElementById('change-due');
const drawerDiv=document.getElementById('drawer');
 

let quotient=0;

let price =19.5;
const currency_units = {
100: "ONE HUNDRED",
20: "TWENTY",
10: "TEN",
5: "FIVE",
1: "ONE",
0.25: "QUARTER",
0.1: "DIME",
0.05: "NICKEL",
0.01: "PENNY"
};


let changeObj={}

let cid =  [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
let cidObject= {}
for (const [key,value] of cid) {
if(value!=0){
  cidObject[key]=value
}


}
  const neededCurrencyUnits={}
 const cid_object_values=Object.keys(cidObject);
  

  
 for (const [key,value] of Object.entries(currency_units)) {
  
        if(cid_object_values.includes(value)){
          neededCurrencyUnits[key]=value
        }
 }
  
  
const total=()=>{
   const tCashInD=Object.values(cidObject).map(a=>Number(a)).reduce((acc,elem)=>acc+elem,0)
     
  return tCashInD
   

}
 
const changeFunction=(input)=>{
changeObj={}
let change =input-price
  
 
 while(change>=0.01){
   
   for(const unitCash of Object.keys(neededCurrencyUnits).sort((a,b)=>b-a).map((a)=>Number(a))){
    console.log(' needed currency units',Object.keys(neededCurrencyUnits));
     if(Object.keys(neededCurrencyUnits).every((element)=> Number(element)>change)){
      console.log('flag  called');
      change_div.innerHTML=`<p>Status: INSUFFICIENT_FUNDS</p>`
      return false
     }
      if(change>=unitCash){
            console.log('unit cah in loop ',unitCash);
          const key= neededCurrencyUnits[unitCash];
          const value=unitCash
          cidObject[key]-=value
        if(cidObject[key]<0){
          cidObject[key]+=value
         
          delete neededCurrencyUnits[unitCash];
          break;
        }
         
        changeObj[key]=changeObj[key]+value || value
     
       change=(change-unitCash).toFixed(2)
     
       break;
      }
   }

 }
    quotient= change;
    
  
}




purchaseBtn.addEventListener('click',()=>{
const cash= Number(document.getElementById('cash').value);


if(cash<price){
  alert("Customer does not have enough money to purchase the item");
  return
}
else if(cash==price){
  change_div.innerHTML="No change due - customer paid with exact cash"
  return
}
else if( total() < (cash-price)){
  console.log('called insufficient funds');
change_div.innerHTML=`<p>Status: INSUFFICIENT_FUNDS</p>`
return
}
else if(total()==(cash-price)){
let innerHtml="Status: CLOSED"

for(const [key,value] of Object.entries(cidObject)){
  innerHtml+=`<p>${key}: $${value}</p>`
}
change_div.innerHTML=innerHtml;

return

}
else {
 let boolean= changeFunction(cash)
 
   if(quotient!=0){
    change_div.innerHTML=`<p>Status: INSUFFICIENT_FUNDS</p>`
    return
   }
   if(boolean!=false){
    let innerHtml= "<p>Status: OPEN</p>"
  for(const [key,value] of Object.entries(changeObj)){
    innerHtml+=`<p>${key}: $${value}</p>`
  }
  change_div.innerHTML=innerHtml;

  let drawHtml='<h2>Change In Drawer</h2>'
 for(const [key,value] of Object.entries(cidObject)){
   drawHtml+=`<p>${key}: $${value}</p>`
 }
drawerDiv.innerHTML=drawHtml;
  boolean=undefined

   }
   


}
 
})



Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

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