Tell us what’s happening:
Hello,
I’m encountering an issue with the last two user stories. Although the output I’m generating matches the expected results, the test cases are still failing. I’ve experimented with different spacing and formatting, but to no avail. While the output appears to be correct, it’s not being accepted by the test cases.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content = "width=device-width, initialscale-1.0">
<title>Cash Register</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="main">
<h1 id="title">Cash Register</h1>
<div id="user-input">
<form>
<label for="cash">Enter cash given by customer</label>
<input type="number" id="cash">
<button id="purchase-btn">Purchase</button>
</form>
</div>
<div id="info">
<div id="change-av"></div>
<div id="change-due"></div>
</div>
</div>
</body>
<script src="script.js"></script>
</html>
/* file: styles.css */
:root{
--color1: #317873;
--color2: #D9D9B1;
}
body{
background-color: var(--color1);
color: var(--color1);
font-family: sans-serif;
}
#title{
color: var(--color2);
}
#main{
height: 80%;
display: flex;
flex-direction: column;
text-align: center;
justify-content: space-around;
}
form{
width: 80%;
margin: 0 auto;
padding: 20px 10px;
border-radius: 15px;
background-color: var(--color2);
display: flex;
flex-direction: column;
align-items: center;
}
input, label, button{
display: block;
width: 30%;
margin: 5px;
}
button{
border: none;
background-color: var(--color1);
color: var(--color2);
padding: 6px 0;
font-weight: 600;
border-radius: 10px;
}
button:hover{
background: linear-gradient(180deg, var(--color1), black);
cursor: pointer;
}
#info{
margin-top: 10px;
display: flex;
justify-content: space-around;
}
#change-av, #change-due{
background-color: var(--color2);
height: auto;
width: 30%;
border-radius: 15px;
padding: 10px;
padding-left: 20px;
}
#change-av p{
text-align: left;
margin: 0;
}
#change-due{
overflow-wrap: break-word;
}
/* file: script.js */
let price = 1.87;
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 cidValues = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
const input = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const ca = document.getElementById('change-av');
const cd = document.getElementById('change-due')
document.querySelector('#title').innerText += ` \nPrice: $${price}`;
ca.innerHTML = '<h3>Change in Drawer</h3>';
cid.forEach((c)=>{
ca.innerHTML += `<p>${c[0]}: ${c[1]}</p>`
})
const getTotal = (arr) => {
let total = 0;
arr.forEach((c)=> {
total += c[1]
});
return total.toFixed(2);
}
let totalChangeAvailable = getTotal(cid);
const runRegister = () => {
if (input.value === ""){
return;
}
const cash = parseFloat(input.value).toFixed(2);
if (cash < price){
alert("Customer does not have enough money to purchase the item");
return ;
}
if(cash == price){
cd.innerText = "No change due - customer paid with exact cash";
return;
}
const difference = Number(cash-price);
if (difference > totalChangeAvailable){
cd.innerText = "Status: INSUFFICIENT_FUNDS"
return;
}
let change = [
['PENNY', 0],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
let i = cidValues.length -1;
let changeDue = difference;
while (i >= 0){
if(changeDue >= cidValues[i] && cid[i][1] >= cidValues[i]){
let x = cidValues[i]
change[i][1] = x + change[i][1];
changeDue = (changeDue - x).toFixed(2);
cid[i][1] = (cid[i][1] - x).toFixed(2);
}
else{
i--;
}
}
change = change
.filter((c) => c[1] > 0)
.map((d) => [d[0], d[1].toFixed(2)]);
if(changeDue > 0 ){
cd.innerText = "Status: INSUFFICIENT_FUNDS"
return;
}
totalChangeAvailable = (totalChangeAvailable - difference).toFixed(2);
if (totalChangeAvailable == 0.00 ){
cd.innerText = "Status: CLOSED";
}
else{ cd.innerText = "Status: OPEN"; }
change.reverse().forEach((c)=>{
cd.innerText += ` ${c[0]}: $${Number(c[1])}`;
})
}
purchaseBtn.addEventListener('click', runRegister);
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