Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My code is running well, but not passing all the test cases at the SAME TIME.
What I meant by same time is that, if I updated the price and cid to the specific test case,

then that particular test case is working, and the remaining tests are being failed.

Someone please look into this and help me.
Thanks in advance!

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initalscale=0">
    <title>Palindrome Checker</title>
    <link rel="stylesheet" href="styles.css"> 
  </head>

  <body>
    <input id="cash"/>
    <div id="change-due"></div>
    <button id="purchase-btn">Purchase</button>

    <script src="./script.js"></script>
  </body>
</html>
/* file: styles.css */

/* file: script.js */
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]];

//finding total cash in drawer and rounding off to 2 decimals
let totalcid=0;

for(let i=0; i<cid.length;i++){
  totalcid+=cid[i][1];
}

totalcid=Math.round(totalcid*100)/100;
console.log(totalcid);

const denomination = [
  ["ONE HUNDRED", 100],
  ["TWENTY", 20],
  ["TEN", 10],
  ["FIVE", 5],
  ["ONE", 1],
  ["QUARTER", 0.25],
  ["DIME", 0.1],
  ["NICKEL", 0.05],
  ["PENNY", 0.01]
]

const arrLength = denomination.length;

const returningChangeArr =[];
const returningChangeAmountArr = [];

const inputCash = document.getElementById('cash');
const changeDue = document.getElementById('change-due');
const purchaseBtn = document.getElementById('purchase-btn');



purchaseBtn.addEventListener('click',()=>{
  const cash = Number(inputCash.value);
  calChange(cash);
})

const calChange = (givenMoney) => {
//check if we can initiate transaction.
let changeMoney = Math.round((givenMoney-price)*100)/100;//calulate the amount of money to be returned
if(givenMoney<price){
    return alert("Customer does not have enough money to purchase the item");
}
else if(changeMoney>totalcid){
  return changeDue.textContent="Status: INSUFFICIENT_FUNDS";
}
else if(givenMoney === price){
  return changeDue.textContent="No change due - customer paid with exact cash";
}else if(changeMoney === totalcid){
  changeDue.textContent=`Status: CLOSED ${cid} \r\n`

  for(let i=0;i<arrLength;i++){
    let noOfNotes = 0;

    while(changeMoney >= denomination[i][1] && cid[arrLength-i-1][1]>0){
      changeMoney = Math.round((changeMoney-denomination[i][1])*100)/100;
      cid[arrLength-i-1][1] = Math.round((cid[arrLength-i-1][1]-denomination[i][1])*100)/100;
      noOfNotes++;
    }
    
    returningChangeArr[i] = noOfNotes;
    returningChangeAmountArr[i] = [denomination[i][0], (returningChangeArr[i]*denomination[i][1])];

    if(returningChangeArr[i] > 0){
      changeDue.textContent += `${denomination[i][0]}: $${returningChangeAmountArr[i][1]} \r\n`;
    }
  }
}
else{
  changeDue.textContent="Status: OPEN \r\n";

  for(let i=0;i<arrLength;i++){
    let noOfNotes = 0;

    while(changeMoney >= denomination[i][1] && cid[arrLength-i-1][1]>0){
        console.log("changeMoneyBefore: ",changeMoney,"denomination: ",denomination[i][0], "cid before: ",cid[arrLength-i-1][1]);
      changeMoney = Math.round((changeMoney-denomination[i][1])*100)/100;
      cid[arrLength-i-1][1] = Math.round((cid[arrLength-i-1][1]-denomination[i][1])*100)/100;
      noOfNotes++;

      console.log("changeMoneyAfter: ",changeMoney, "denomination: ",denomination[i][0], "cid after: ",cid[arrLength-i-1][1]);
    }
    returningChangeArr[i] = noOfNotes;
    returningChangeAmountArr[i] = [denomination[i][0], (returningChangeArr[i]*denomination[i][1])];

    if(returningChangeArr[i] > 0){
      changeDue.textContent += `${denomination[i][0]}: $${returningChangeAmountArr[i][1]} \r\n`;
    }
  }
}

for(let i=0;i<cid.length;i++){
    cid[i]=cid[i].join(": ");
  }
  cid = cid.join(" ");
    
}

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

This usually means that code depends on some global variables, other than the expected cid, price and #cash element. When manually changing values to check test case, the preview will be reloaded and all code will execute. During tests page is not reloaded, ending up with part of code stuck with the initial values.

1 Like

Thank you, it’s working now after the changes.

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