Tell us what’s happening:
Hi Guys, I have like 5 days working on this, but i still can not manage to make it work, it seems like there a lot of things i still don’t understand on this project. Now I want your help please , so i can finish this project.
I have issues started from step 11.
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">
<link rel="stylesheet" href="styles.css">
<title>Cash Register</title>
</head>
<body>
<div class="container">
<h1>Cash Register App</h1>
<div class="change-details" id="change-due"></div>
<form action="">
<label for="cash">Enter cash from Customer:</label>
<input id="cash" type="number" inputmode="decimal" >
<button type="button" id="purchase-btn">Purchase</button>
</form>
<div class="total-price" id="total-price">
</div>
<div class="drawer">
<div class="drawer-details" id="drawer-details">
<h2>Change in drawer:</h2>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
let price = 19.5;
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]
];
let currencyUnit = [
["PENNY", 0.01],
["NICKEL", 0.05],
['DIME', 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
['HUNDRED', 100],
]
const purchaseBtn = document.getElementById("purchase-btn")
const drawerContainer = document.getElementById("drawer-details");
const changeDetails = document.getElementById("change-due");
const totalPrice = document.getElementById("total-price")
purchaseBtn.addEventListener("click", (event) =>{
event.preventDefault();
let total = 0;
for(let el of cid){
total += parseInt(el[1] * 100) / 100;
}
let statusItem = "";
let cash = document.getElementById("cash").value;
const changeDue = Number(cash.value*100 - price*100) / 100;
if(parseFloat(cash) < price){
alert("Customer does not have enough money to purchase the item");
return;
}
if(parseFloat(cash) === price){
changeDetails.innerText = "No change due - customer paid with exact cash"
return;
}
if(cash > price){
if(changeDue > total){
statusItem = "INSUFFICIENT_FUNDS";
changeDetails.innerText = `Status: ${statusItem}`;
}else if(total === changeDue){
statusItem = "CLOSED";
changeDetails.innerText = `Status: ${statusItem}`;
}else{
statusItem = "OPEN";
check_remaining_cash(cid, changeDue, currencyUnit);
}
}
});
const check_remaining_cash = (cid, amountDue, arr) =>{
let status = "";
let total = 0;
for(let el of cid){
total += parseInt(el[1] * 100) / 100;
}
let newArr = [];
for(let i = cid.length - 1; i >= 0; i--){
let [itemName, amount] = cid[i];
let itemValue = arr[itemName];
let refundAmount = 0;
while(amount >= itemValue && itemValue <= amountDue){
amountDue = parseFloat((amountDue - itemValue).toFixed(2));
amount = parseFloat((amount - itemValue).toFixed(2));
refundAmount = parseFloat((refundAmount + itemValue).toFixed(2));
};
if(refundAmount > 0){
newArr.push([itemName, refundAmount]);
};
};
if(amountDue > 0){
status = "INSUFFICIENT_FUNDS";
changeDetails.innerText = `Status: ${status}`;
return;
}
if(total === amountDue){
status = "CLOSED";
newArr = [...cid]
}else{
status = "OPEN";
}
const displayChange = newArr.map(([itemName, amount]) =>
`${itemName}: $${amount.toFixed(2)}`).join(' ')
changeDetails.innerText = `Status: ${status} ${displayChange}`;
};
function update_drawer(arr){
totalPrice.innerHTML += `<p>$${price}</p>`;
for(let item of arr){
drawerContainer.innerHTML += `<p>${item[0][0].toUpperCase() + item[0].slice(1).toLowerCase()} : $${item[1]}</p>`
}
}
update_drawer(cid);
/* file: styles.css */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color: #023047;
color: white;
}
.container{
display: flex;
flex-direction: column;
gap: 10px;
margin: 50px auto;
width: 50%;
padding: 10px;
text-align: center;
justify-content: center;
align-items: center;
}
.change-details{
display: flex;
flex-direction: column;
margin: 15px auto;
text-align: center;
align-items: center;
}
form{
display: flex;
flex-direction: column;
width: 50%;
align-items: center;
justify-content:center;
text-align: center;
}
input{
font-size: 15px;
margin: 5px 0;
height: 30px;
}
label{
font-size: 19px;
font-family: sans-serif;
text-align:center;
}
button{
padding: 8px 16px;
background-color: #fb8500;
color: white;
font-size: 18px;
font-family: sans-serif;
border: 3px solid #ffb703;
border-radius: 7px;
text-align: center;
margin: 10px auto;
display: inline-block;
cursor: pointer;
transition: transform 0.3 ease;
}
button:active{
transform: translateY(-3px);
}
.total-price{
font-size: 20px;
font-family: sans-serif;
margin: 20px;
background-color:#023047;
padding: 5px 15px;
border: 5px solid #fb8500;
border-radius: 5px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align:center;
width: 160px;
max-width: 200px;
height: auto;
}
.drawer{
background-color:#219ebc;
color: #444;
width: 300px;
height: auto;
font-size: 18px;
border-radius: 20px 20px 0 0;
}
.drawer-details{
background-color:white;
width: 50%;
height: auto;
margin: 12px auto;
font-size: 16px;
display:flex;
flex-direction: column;
gap: 4px;
padding: 10px;
}
h2{
font-size:16px;
text-align: center;
}
#equal-message{
font-size: 21px;
}
@media (max-width: 500px){
h1{
font-size: 18px;
}
form{
display: flex;
flex-direction: column;
width: 100%;
align-items: center;
justify-content:center;
text-align: center;
}
label{
font-size: 15px;
}
.container{
width: 70%;
}
button{
padding: 4px 8px;
background-color: #fb8500;
color: white;
font-size: 18px;
font-family: sans-serif;
border: 3px solid #ffb703;
border-radius: 7px;
text-align: center;
margin: 5px auto;
display: inline-block;
cursor: pointer;
transition: transform 0.3 ease;
box-shadow: 0 4px 9px 0;
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register