Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

The code seems to be working, I just can’t understand why the test is not accepting it

Your code so far

<!-- file: index.html -->

const cash=document.querySelector("#cash");
const change_due=document.getElementById("change-due");
const btn=document.getElementById("purchase-btn");
const change_due_box=document.getElementById("change-due-box");
const cid_txt=document.querySelector(".register_cont_b");
const total_txt=document.querySelector(".register_cont_a");

let price = 19.5;
/*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.5],
  ['NICKEL', 0],
  ['DIME', 1],
  ['QUARTER', 1],
  ['ONE', 1],
  ['FIVE', 0],
  ['TEN', 0],
  ['TWENTY', 0],
  ['ONE HUNDRED', 0]
];

let cid_cent=[
  ['PENNY', 0],
  ['NICKEL', 0],
  ['DIME', 0],
  ['QUARTER', 0],
  ['ONE', 0],
  ['FIVE', 0],
  ['TEN', 0],
  ['TWENTY', 0],
  ['ONE HUNDRED', 0]
  ];

let change = 0;
let change_arr= [0,0,0,0,0,0,0,0,0];
let arr_val= [1,5,10,25,100,500,1000,2000,10000]

//convert to cent
for(let i=8; i>-1;i--){
  cid_cent[i][1]=Math.round(cid[i][1]*100)
}
let price_c=Math.round(price*100);
//--



const calculate_change = (c) => {
  change_due_box.style.display="block";
  change=c-price_c;
  for(let i = 8; i>-1;i--){console.log(change);
    while(change-arr_val[i]>=0&&cid_cent[i][1]>0){
      cid_cent[i][1]=cid_cent[i][1]-arr_val[i];
      change-=arr_val[i];
      change_arr[i]=change_arr[i]+1};
      };
      
      check_funds(change);
 }

const cbox_update = () => {
  change_due.innerHTML=`<strong>Status: ${isClose()}</strong>`;
  
  for(let i=8; i>-1;i--){
    change_arr[i]>0?change_due.innerHTML+=
    `<div>${cid[i][0]}: $${change_arr[i]*arr_val[i]/100}</div>`:false;
};
}

const isClose = () => {
 return cid_cent.every(row=>row[1]==0)?"CLOSED":"OPEN";
}

const check_funds = (c) => {
  if(c>0){  
  change_due.textContent="Status: INSUFFICIENT_FUNDS";
  }
  else{
    cbox_update();
    update();}
  }

const click = () => {
  change_arr= [0,0,0,0,0,0,0,0,0];
  if(cash.value<price){
    alert("Customer does not have enough money to purchase the item")
          }
  else if(cash.value==price){
    change_due.textContent="No change due - customer paid with exact cash";
    change_due_box.style.display="block";
     }
  else
  {
    calculate_change(cash.value*100);
  }
  }

const update = () =>{
for(let i=8; i>-1;i--){
  cid[i][1]=cid_cent[i][1]/100;}
total_txt.innerHTML=`<strong>~ Total: $${price} ~</strong>`
cid_txt.innerHTML=
`<p><strong>Change In Drawer:</strong><br>
Pennies: $${cid[0][1]}<br>
Nickels: $${cid[1][1]}<br>
Dimes: $${cid[2][1]}<br>
Quarters: $${cid[3][1]}<br>
Ones: $${cid[4][1]}<br>
Fives: $${cid[5][1]}<br>
Tens: $${cid[6][1]}<br>
Twenties: $${cid[7][1]}<br>
Hundreds: $${cid[8][1]}<br></p>
`;
}

window.addEventListener("load",update);
btn.addEventListener("click",click);
/* 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/132.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hi there and welcome to our community!

It looks like your code was too long to be ported over automatically when you created this post. You’ll need to take an additional step, editing your post and pasting your full code in between the sets of triple backticks where indicated.

Thanks :slight_smile:

oh sorry, it’s my first time posting so I didn’t know.
I edited it like you said. thnx!

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

thanks a lot, it managed to pass when i rewrote it with global variables in mind.