Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

When price is 19.5, the value in the #cash element is 20, cid is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the #purchase-btn element is clicked, the value in the #change-due element should be Status: CLOSED PENNY: $0.5 this test case is not passed. when i print cid array then value of ‘PENNY’ in cid should be 0.5 but it prints 0.01 instead.

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">
  <title>Cash Register App</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div id="app">
    <h1>Cash Register App</h1>
    <label for="cash">Enter cash amount:</label>
    <input type="number" id="cash">
    <div id="change-due">Change Due:</div>
    <button id="purchase-btn">Purchase</button>
  </div>
  <script src="./script.js"></script>
</body>
</html>

/* file: styles.css */
body {
  font-family: Arial, sans-serif;
  text-align: center;
}

#app {
  margin-top: 50px;
}

input[type="number"] {
  padding: 10px;
  margin-bottom: 10px;
}

button {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

#change-due {
  margin-top: 20px;
}

/* file: script.js */
let price = 1.87;
let cid = [
  ["PENNY", 0.5],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];
document.addEventListener("click",()=>{
  
  const cashInput = document.getElementById('cash');
  const changeDueDiv = document.getElementById('change-due');
  const purchaseBtn = document.getElementById('purchase-btn');
  let cash=parseFloat(document.getElementById("cash").value)
  let changeDue = cash - price;
  if(changeDue<0){
    alert("Customer does not have enough money to purchase the item")
    return;
  }
  if(changeDue===0){
    changeDueDiv.textContent="No change due - customer paid with exact cash"
    return;
  }
  const totalCID = getTotalCID(cid);
    function getTotalCID(cid) {
    return cid.reduce((total, arr) => total + arr[1], 0);
  }
  if (changeDue > totalCID) {
      changeDueDiv.textContent= "Status: INSUFFICIENT_FUNDS";
    } else if (changeDue === totalCID) {
       changeDueDiv.textContent ="Status: CLOSED";
    } else {
      const changeArr = calculateChange(changeDue, cid);
      if (changeArr.length === 0 || changeDue > getTotalCID(changeArr)) {
        changeDueDiv.textContent= "Status: INSUFFICIENT_FUNDS";
      } else {
         changeDueDiv.textContent="Status: OPEN " + changeArr.map(arr => `${arr[0]}: $${arr[1]}`).join(" ");
      }
    }
    function calculateChange(changeDue, cid) {
    const currencyValues = {
      "ONE HUNDRED": 100,
      "TWENTY": 20,
      "TEN": 10,
      "FIVE": 5,
      "ONE": 1,
      "QUARTER": 0.25,
      "DIME": 0.1,
      "NICKEL": 0.05,
      "PENNY": 0.01
    };
    let changeArr = [];
    for(let i=0;i<cid.length;i++){
      console.log("currency-> "+cid[i][0]+" tatal-> "+cid[i][1])
    }
    for (let i = cid.length - 1; i >= 0; i--) {
      const currencyName = cid[i][0];
      var currencyAmount = parseFloat(cid[i][1]);
      console.log("amt is "+currencyAmount)
      const currencyValue = parseFloat(currencyValues[currencyName]);
      let count = 0;
      while (changeDue >= currencyValue && currencyAmount > parseFloat(0)) {
        changeDue =parseFloat(changeDue- currencyValue);
        // console.log("baki amt5"+currencyAmount)
        // console.log("baki amt6"+currencyValue)
        currencyAmount =parseFloat(currencyAmount- currencyValue);
        count++;
        changeDue = Math.round(changeDue * 100) / 100; // Round to avoid floating point errors
        console.log("baki"+changeDue)
        console.log("baki amt"+currencyAmount)
      }
      if (count > 0) {
        changeArr.push([currencyName, count * currencyValue]);
        
      }
    }
    
    return changeArr;
    
    
  }

})


Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

To fix the problem, you should convert all your currency values to their respective integer representations (e.g., convert dollars to cents) before performing any calculations. After calculating the change, you can convert it back to the appropriate currency format for display purposes.

let price = 187; // Price in cents (1.87 dollars)
let cid = [
[“PENNY”, 50], // 0.50 dollars converted to 50 cents
[“NICKEL”, 205], // 2.05 dollars converted to 205 cents
[“DIME”, 310], // 3.10 dollars converted to 310 cents
[“QUARTER”, 425], // 4.25 dollars converted to 425 cents
[“ONE”, 9000], // 90.00 dollars converted to 9000 cents
[“FIVE”, 5500], // 55.00 dollars converted to 5500 cents
[“TEN”, 2000], // 20.00 dollars converted to 2000 cents
[“TWENTY”, 6000], // 60.00 dollars converted to 6000 cents
[“ONE HUNDRED”, 10000] // 100.00 dollars converted to 10000 cents
];

document.addEventListener(“DOMContentLoaded”, () => {
const cashInput = document.getElementById(‘cash’);
const changeDueDiv = document.getElementById(‘change-due’);
const purchaseBtn = document.getElementById(‘purchase-btn’);

purchaseBtn.addEventListener(“click”, () => {
let cash = parseFloat(cashInput.value) * 100; // Convert cash input to cents
let changeDue = cash - price;

if (changeDue < 0) {
  alert("Customer does not have enough money to purchase the item");
  return;
}

if (changeDue === 0) {
  changeDueDiv.textContent = "No change due - customer paid with exact cash";
  return;
}

const totalCID = getTotalCID(cid);

function getTotalCID(cid) {
  return cid.reduce((total, arr) => total + arr[1], 0);
}

if (changeDue > totalCID) {
  changeDueDiv.textContent = "Status: INSUFFICIENT_FUNDS";
} else if (changeDue === totalCID) {
  changeDueDiv.textContent = "Status: CLOSED";
} else {
  const changeArr = calculateChange(changeDue, cid);
  if (changeArr.length === 0 || changeDue > getTotalCID(changeArr)) {
    changeDueDiv.textContent = "Status: INSUFFICIENT_FUNDS";
  } else {
    changeDueDiv.textContent = "Status: OPEN " + changeArr.map(arr => `${arr[0]}: $${(arr[1] / 100).toFixed(2)}`).join(" ");
  }
}

});

function calculateChange(changeDue, cid) {
const currencyValues = {
“ONE HUNDRED”: 10000,
“TWENTY”: 2000,
“TEN”: 1000,
“FIVE”: 500,
“ONE”: 100,
“QUARTER”: 25,
“DIME”: 10,
“NICKEL”: 5,
“PENNY”: 1
};
let changeArr = ;

for (let i = cid.length - 1; i >= 0; i--) {
  const currencyName = cid[i][0];
  const currencyAmount = cid[i][1];
  const currencyValue = currencyValues[currencyName];

  let count = 0;
  while (changeDue >= currencyValue && currencyAmount > 0) {
    changeDue -= currencyValue;
    currencyAmount -= currencyValue;
    count++;
  }

  if (count > 0) {
    changeArr.push([currencyName, count * currencyValues[currencyName]]);
  }
}
return changeArr;

}
});

In this modified code:

  • Prices and cash amounts are represented in cents to avoid floating-point arithmetic issues.
  • When displaying the change, the values are converted back to dollars for readability.
  • The event listener for the “click” event on the purchase button is added after the DOM content is loaded to ensure that the elements are accessible.

still not passed all test cases can you give complete code for this project??

no, it’s a final project, you need to complete this yourself —you can ask for help, but having someone else complete the project for you is severely inappropriate

i am not able to pass last test case can you help me??

it prints only Status: CLOSED, you need to have also the amount of cash given to the client