Cash Register Project Bug

Hello, I need some help my Cash Register Project Solution. I tested my code locally, and results match all expected results of user stories, but when I run the test, there’s always some user stories failed to pass (the user stories fail varies when I change the price and cid values). Below is my code:

const changeText = document.getElementById("change-due");
const cashElement = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const priceText = document.getElementById("price");
const cashText = document.getElementsByClassName("currency");
const cashTextArray = [...cashText]; /*turn cashText into an array*/


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]
];

const change = [
  ["ONE HUNDRED", 0],
  ["TWENTY", 0],
  ["TEN", 0],
  ["FIVE", 0],
  ["ONE", 0],
  ["QUARTER", 0],
  ["DIME", 0],
  ["NICKEL", 0],
  ["PENNY", 0], 
];

const currency = [
  {
    name: "ONE HUNDRED",
    unitInCents: 10000,
    count: 0,
    currentAmount: 0
  },
  {
    name: "TWENTY",
    unitInCents: 2000,
    count: 0,
    currentAmount: 0
  },
  {
    name: "TEN",
    unitInCents: 1000,
    count: 0,
    currentAmount: 0
  },
  {
    name: "FIVE",
    unitInCents: 500,
    count: 0,
    currentAmount: 0
  },
  {
    name: "ONE",
    unitInCents: 100,
    count: 0,
    currentAmount: 0
  },
  {
    name: "QUARTER",
    unitInCents: 25,
    count: 0,
    currentAmount: 0
  },
  {
    name: "DIME",
    unitInCents: 10,
    count: 0,
    currentAmount: 0
  },
  {
    name: "NICKEL",
    unitInCents: 5,
    count: 0,
    currentAmount: 0
  },
  {
    name: "PENNY",
    unitInCents: 1,
    count: 0,
    currentAmount: 0
  }
]

//Link currency to cid
const cidReverse = [...cid].reverse();
for (let i = 0; i < currency.length; i++) {
  currency[i].currentAmount = Math.round((cidReverse[i][1]*100));
}


priceText.textContent = `$${price}`;
updateDrawerText(); 

purchaseBtn.addEventListener("click", () => { 
  const cashValue = Number(cashElement.value);

  if(cashValue < price) {
  alert("Customer does not have enough money to purchase the item");
  } else if(cashValue === price) {
    changeText.textContent = "No change due - customer paid with exact cash";
  } else {
    resetChange();
    checkCashDrawer(cashValue);
    updateDrawerText(); 
  }
  cashElement.value = "";
})

function resetChange() {
  for(const ch of change) {
    ch[1] = 0;
  }
}


let currencyCopy = JSON.parse(JSON.stringify(currency)); //create a deep copy of currency for temporary save of currentAmount change

function checkCashDrawer(cashValue) {  
  let changeDueInCents = parseInt(cashValue*100 - price*100);
  //for test
  console.log(changeDueInCents);

  for (let i = 0; i < currencyCopy.length; i++) {  
    currencyCopy[i].currentCount = parseInt(currencyCopy[i].currentAmount / currencyCopy[i].unitInCents); 
    currencyCopy[i].count = Math.floor(changeDueInCents / currencyCopy[i].unitInCents);
  //for test
    console.log(currencyCopy[i].count);
    console.log(currencyCopy[i].currentCount);


  if(currencyCopy[i].count >= currencyCopy[i].currentCount) {
    changeDueInCents -= currencyCopy[i].currentAmount; 
    currencyCopy[i].currentAmount = 0;
    currencyCopy[i].currentCount = 0;
    } else if (currencyCopy[i].count < currencyCopy[i].currentCount && currencyCopy[i].count > 0) {
    changeDueInCents -= currencyCopy[i].count * currencyCopy[i].unitInCents;
    currencyCopy[i].currentAmount -= currencyCopy[i].count * currencyCopy[i].unitInCents;
    currencyCopy[i].currentCount -= currencyCopy[i].count;
    } 
    //for test
    console.log(currencyCopy[i]);
    console.log(changeDueInCents);
    console.log(currencyCopy[i].currentAmount);
    console.log(currencyCopy[i].currentCount)
  }

  if (changeDueInCents === 0) {  
    updateCurrency(changeDueInCents);
    //updateCid();
  }  

  updateChangeText(changeDueInCents);
}


function updateCurrency() { 
  for(let i = 0; i < currency.length; i++){
    if(currency[i].currentAmount !== currencyCopy[i].currentAmount) {
      change[i][1] = (currency[i].currentAmount - currencyCopy[i].currentAmount)/100
    };
      currency[i].currentAmount = currencyCopy[i].currentAmount;
      currency[i].currentCount = currencyCopy[i].currentCount;
  }
}

/*function updateCid() {
  for (let i = 0; i < currency.length; i++) {
    cidReverse[i][1] = currency[i].currentAmount/100;
  }
  cid = [...cidReverse].reverse();
}*/

function updateDrawerText() {
  let currencyReverse = [...currency].reverse(); // [...currency] create a shallow copy of currency array so that .reverse() will not modify the original array.
  for(let i = 0; i < cashTextArray.length; i++) {    
      cashTextArray[i].textContent = `$${currencyReverse[i].currentAmount/100}`
  }
}


function updateChangeText(changeDueInCents){
  let cashDrawerStatus = "OPEN";
  const changeOffer = change.filter(item => item[1]);

  if (changeDueInCents !== 0) {
     cashDrawerStatus = "INSUFFICIENT_FUNDS";
  } else if (currency.every(item => item.currentAmount === 0)) { 
    cashDrawerStatus = "CLOSED"
  }
  changeText.innerHTML = `
    <p>Status: ${cashDrawerStatus}</p>`

  changeOffer.forEach((item) => {
    const paragraph = document.createElement('p');
    paragraph.textContent = `${item[0]}: $${item[1]}`;
    changeText.appendChild(paragraph);
    });
}
1 Like

do not do anything in the global scope. anything that influences that output needs to happen when you press the button

2 Likes

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').