Js cash register

i am having problems with the tests, the test will only return true if i set the cid variable to the one in the test.

my code:

let cash = document.getElementById('cash');
let changeDue = document.getElementById('change-due');
let perchaseBtn = document.getElementById('purchase-btn');
let price = 19.5;
let cashValueArr = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01]
let openRegisterResult = ""

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 revCid = cid.reverse();

perchaseBtn.addEventListener('click', () => {
  let changeToGive = cash.value - price;
  let originalChangeToGive = cash.value - price;
  let totalCash = Number(cid.reduce((sum, value) => sum + value[1], 0).toFixed(2));

  if (cash.value < price){
  return alert(`Customer does not have enough money to purchase the item`)
  } else if (cash.value == price){
    return changeDue.innerText = `No change due - customer paid with exact cash`;
  }

  for (const key in revCid){
  let face = revCid[key][0];
  let value = revCid[key][1];
  let coinAmount = Number((value / cashValueArr[key]).toFixed(0));
  let currencyToReturn = 0;
   
  while(changeToGive >= cashValueArr[key] && coinAmount > 0){
    changeToGive -= cashValueArr[key];
    changeToGive = Number(changeToGive.toFixed(2));
    coinAmount--;
    currencyToReturn++;
    revCid[key][1] -= cashValueArr[key]
}
  if (currencyToReturn > 0){
  openRegisterResult += `${face}: $${currencyToReturn * cashValueArr[key]} `;
  }
  }
  console.log(revCid)
  if (totalCash < originalChangeToGive){
   return changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
  }else if (totalCash == originalChangeToGive) {
    return changeDue.innerText = `"Status: CLOSED ${openRegisterResult}"`;
  }
  
  if (changeToGive > 0){ 
  return changeDue.innerText = "Status: INSUFFICIENT_FUNDS"
  }
  
  return changeDue.innerText = `"Status: OPEN ${openRegisterResult}"`;
 })

You should generally avoid global variables you don’t need. This global variable will cause problems for you.

how should i replace cid.reverse()

I dunno, there’s lots of ways you might. What comes to mind for you?

This also probably shouldn’t be a global variable.

Function arguments and return values are there so you don’t have to put so much in the global space

so i should put them in the add event listener??

I’d put those variables inside of your functions, yes.

ok dude thanks for the help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.