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