Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I don’t know what is wrong with my code. Some of the test it does not pass but with manual check it shows the right result. I just can’t see the problem. Can someone please help?

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">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML 5 Boilerplate</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <form>
      <label for="cash">Cash from Customer</label>
      <input type="number" name="cash" id="cash" step="0.01">
      <button id="purchase-btn" type="submit">Purchase</button>
      <p id="change-due"></p>

    </form>
    
    


    <script src="script.js"></script>
  </body>
</html>

/* file: script.js */
const cashInput = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeTextBox = document.getElementById("change-due");
let price = 11.95;
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 cashRegister = () => {
  let cash = Number(cashInput.value);
  let currencyUnitName = [];
  let currencyUnitTotal = [];
  let curencyUnitValue = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  let total = 0;
  let change = cash - price;
  

  const decimalFix = (num) => parseFloat(num.toFixed(2)); 


  cid.forEach((el) => {
    currencyUnitName.push(el[0]);
    currencyUnitTotal.push(el[1]);
  });

  total = currencyUnitTotal.reduce((a, b) => a + b, 0);
  total = decimalFix(total);
  change = decimalFix(change);


  let unitsArr = [];
  let unitsSummArr = [];


  const calculate = () => {
    for(let i = cid.length - 1; i>=0 ; i--){
      let unitGiven = '';
      let unitSumm = 0;
      
      while(change >= curencyUnitValue[i] && currencyUnitTotal[i] > 0 ){
        change -= curencyUnitValue[i]; 
        currencyUnitTotal[i] -= curencyUnitValue[i];
        total -= curencyUnitValue[i];
        total = decimalFix(total);
        unitSumm += curencyUnitValue[i];
        unitGiven = currencyUnitName[i];
        unitSumm = decimalFix(unitSumm);
      };
      if(unitSumm !== 0 || unitGiven !== ""){
        unitsArr.push(unitGiven);
        unitsSummArr.push(unitSumm);
      }
      }
      console.log(total, currencyUnitTotal);
    };

if(cash < price){
  alert("Customer does not have enough money to purchase the item");
}else if(cash === price){
  alert("No change due - customer paid with exact cash");
}else if(total < change){
  changeTextBox.textContent = "Status: INSUFFICIENT_FUNDS";
}else if(total === change){
  calculate();
  changeTextBox.textContent = "Status: CLOSED";
  for(let i = 0; i<unitsArr.length; i++){
  changeTextBox.textContent += ` ${unitsArr[i]}: $${unitsSummArr[i]}`;
  }
}else if(total > change) {
  calculate();
  changeTextBox.textContent = `Status: OPEN `;
  for(let i = 0; i<unitsArr.length; i++){
  changeTextBox.textContent += ` ${unitsArr[i]}: $${unitsSummArr[i]}`;
  }
}

};
  


purchaseBtn.addEventListener("click", cashRegister);

/* 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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

I don’t see any logic with your event listener? it should have an object.

What do you mean? Event listener is calling the function cashRegister.

Can you just call it likefunctionName()?

does it want an alert? read carefully

1 Like

Thanx :smile: I did not see that