Tell us what’s happening:
hi, i would like you to check my code, please, all of the test are correct except 17, 18 and 19. It doesn’t make sense to me because test number 16 is correct which is the one related to test 17, also, I already tried some cases and it is still incorrect. The test 18 and 19 are similar, for these cases, i also tried some inputs, the answer is correct, but the test aren’t
the code is bellow
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</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main>
<div id="content">
<h1>Cash Register</h1>
<div id="entry">
<div id="change-due"></div>
<div id="input">
<label for="cash">Enter cash from customer:</label>
<input type="number" id="cash" min="0">
<button id="purchase-btn">Purchase</button>
</div>
</div>
<div id="cash-register">
<div id="left-part">
<div id="price"></div>
<div id="cash-register-btns">
<div class="cash-register-btn">9</div>
<div class="cash-register-btn">8</div>
<div class="cash-register-btn">7</div>
<div class="cash-register-btn">6</div>
<div class="cash-register-btn">5</div>
<div class="cash-register-btn">4</div>
<div class="cash-register-btn">3</div>
<div class="cash-register-btn">2</div>
<div class="cash-register-btn">1</div>
<div class="cash-register-btn open"></div>
<div class="cash-register-btn cero">0</div>
<div class="cash-register-btn close"></div>
</div>
</div>
<div id="cash-in-drawer">
<p>Change In Drawer</p><br>
<div>Total in Currency --- Units</div><br>
<div id="cid">
</div>
</div>
</div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
// return change to the customer based on the price of the item, the amount of cash provided by the customer, and the amount of cash in the cash drawer
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 price = 19.5;
// let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
// let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
const currencyValues = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
const changeText = document.getElementById('change-due');
const input = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const textPrice = document.getElementById('price');
textPrice.textContent = `Total: $${price}`;
const cidContainer = document.getElementById('cid');
let quantityCurrencies = [];
for (let i = 0; i < currencyValues.length; i++) {
quantityCurrencies.push(Math.round(cid[i][1] / currencyValues[i]));
}
// Functions -------------------------------------------------------------------------
const verifyInput = () => {
let sumCashInDrawer = parseFloat((cid.reduce((acc, el) => acc + el[1], 0)).toFixed(2))
let inputValue = parseFloat((parseFloat(input.value)).toFixed(2));
let change = parseFloat((parseFloat(inputValue - price)).toFixed(2));
if (change === sumCashInDrawer){
return true;
}
if (!inputValue) {
alert('Please, enter some money');
} else if(inputValue < price) {
alert('Customer does not have enough money to purchase the item');
} else if (change > sumCashInDrawer) {
displayStatus()
} else {
return true;
}
}
// function that updates the visual screen on the HTML
const updateScreenCashInDrawer = (arr) => {
cidContainer.innerHTML = '';
for (let i = 0; i < cid.length; i++) {
const quantity = cid[i][1] / currencyValues[i];
cidContainer.innerHTML += `<div><span>${cid[i][0].charAt(0) + cid[i][0].slice(1).toLowerCase()}</span>: $${cid[i][1]} --- ${Math.round(quantity)} u</div>`
}
}
updateScreenCashInDrawer(cid); // call function to see the initial balance in the cash drawer
const calculateChange = (customerMoney) => {
let changeDue = parseFloat((customerMoney - price).toFixed(2));
let possible = false;
// the flag possible was declared to verify if it is possible to return the change with the denominations available
if (changeDue == 0) {
possible = true;
}
for (let i = (cid.length) - 1; i >= 0; i--) {
if (changeDue >= currencyValues[i]) {
let sumToLeft = 0
for (let j = i; j >= 0; j--) {
sumToLeft += cid[j][1];
}
if (sumToLeft >= changeDue) {
possible = true;
}
}
}
if (possible) {
for (let i = (cid.length) - 1; i >= 0; i--) {
let coinsAvailable = quantityCurrencies[i];
let coinsUsed = 0;
if (changeDue >= currencyValues[i] && quantityCurrencies[i] != 0) {
console.log(cid[i][0])
while (coinsAvailable > 0 && changeDue >= currencyValues[i]) {
coinsAvailable -= 1;
coinsUsed += 1;
quantityCurrencies[i] = coinsAvailable;
changeDue -= parseFloat(currencyValues[i].toFixed(2));
changeDue = parseFloat(changeDue.toFixed(2));
}
cid[i][1] = parseFloat((quantityCurrencies[i] * currencyValues[i]).toFixed(2));
console.log(changeDue)
changeText.innerHTML += `<p>${cid[i][0]}: $${parseFloat(parseFloat(currencyValues[i] * coinsUsed).toFixed(2))}</p>`
console.log(quantityCurrencies)
} else {
console.log(`${currencyValues[i]} no se uso`)
}
}
} else {
changeText.innerHTML = '';
changeText.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
}
}
// function that displays the status
const displayStatus = () => {
changeText.innerHTML = ''
const totalCashInDrawer = parseFloat(cid.reduce((acc, el) => acc + el[1], 0).toFixed(2));
const change = parseFloat((input.value - price).toFixed(2));
if (totalCashInDrawer < change) {
changeText.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
} else if (totalCashInDrawer === change) {
changeText.innerHTML += `<p>Status: CLOSED</p>`;
} else if (totalCashInDrawer > change) {
changeText.innerHTML += `<p>Status: OPEN</p>`;
}
if (input.value == price) {
changeText.textContent = 'No change due - customer paid with exact cash';
}
}
// Event Listeners -------------------------------------------------------------------------
purchaseBtn.addEventListener('click', () => {
updateScreenCashInDrawer(cid);
if (verifyInput()) {
displayStatus()
calculateChange(parseFloat((parseFloat(input.value)).toFixed(2)));
updateScreenCashInDrawer(cid);
} else {
console.log('invalid input')
}
})
input.addEventListener('keydown', (e) => {
if (e.key == 'Enter') {
updateScreenCashInDrawer(cid);
if (verifyInput()) {
displayStatus()
calculateChange(parseFloat((parseFloat(input.value)).toFixed(2)));
updateScreenCashInDrawer(cid);
}
else {
console.log('invalid input')
}
}
})
/* file: styles.css */
* {
box-sizing: border-box;
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
margin: 0;
padding: 0;
}
body {
background-color: rgb(30, 33, 41);
color: white;
}
main {
width: 100vw;
height: 100vh;
text-align: center;
align-content: center;
}
#content {
max-width: 700px;
margin: auto;
}
h1 {
font-size: 50px;
margin-bottom: 30px;
}
#entry {
display: flex;
flex-direction: column;
gap: 2rem;
margin-bottom: 30px;
}
#input {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}
#input > label {
font-size: 20px;
}
#input > input {
font-size: 20px;
width: 40%;
padding: 8px 5px;
}
#input > button {
font-size: 20px;
width: 20%;
padding: 5px 3px;
background-color: rgb(253, 226, 114);
border: 4px solid rgb(251, 201, 0);
cursor: pointer;
transition-duration: .3s;
}
#input > button:hover {
background-color: rgb(252, 175, 87);
border: 4px solid rgb(255, 153, 0);
}
#cash-register {
width: 100%;
height: 450px;
background-color: rgb(74, 74, 74);
display: flex;
border-radius: 15px;
}
#left-part {
width: 40%;
margin: 40px 20px 40px 40px;
}
#price {
width: 100%;
background-color: black;
padding: 10px 20px;
font-size: 20px;
text-align: right;
color: rgb(0, 255, 0);
border-radius: 10px;
}
#cash-in-drawer {
width: 60%;
margin: 40px 40px 40px 20px;
background-color: aliceblue;
background-color: black;
color: rgb(0, 255, 0);
border-radius: 10px;
text-align: right;
padding: 30px 30px;
font-size: 17px;
}
.cash-register-btn {
height: 50px;
width: 50px;
background-color: rgb(101, 101, 101);
border: 3px solid rgb(43, 43, 43);
color: rgb(255, 255, 255);
align-content: center;
cursor: pointer;
}
#cash-register-btns {
margin: 40px auto;
display: grid;
position: relative;
left: -10px;
grid-template-columns: repeat(3, 1fr);
width: 70%;
justify-items: center;
gap: 1rem 1rem;
}
.open {
background-color: rgb(0, 255, 0);
}
.close {
background-color: red;
}
#change-due {
font-size: 21px;
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register