Tell us what’s happening:
The tests are confusing me as I can get the desired display text when I manually enter in the values but they still register as failing.
Something as simple as copy-pasting the “INSUFFICIENT_FUNDS” to replace what I typed out caused most of those related tests to pass but then caused the “OPEN” related tests to fail when they were passing before that single change.
Is there a certain formatting requirement I’ve missed?
*After a bit of further testing it seems to be affected by the declarations of price
and cid
, though I’m still unsure why they’re causing this behavior.
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="style.css">
<link rel="shortcut icon" href="https://www.svgrepo.com/show/532403/cash-register.svg" type="image/x-icon">
<title>Cash Counter</title>
</head>
<body>
<div id="screen">
</div>
<section id="cash-register">
<input type="number" id="cash">
<button id="purchase-btn">Purchase</button>
<div id="change-due"></div>
</section>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
let price = 1.87;
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]
];
cid= [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
const cashValues = {
'PENNY': .01,
'NICKEL': .05,
'DIME': .1,
'QUARTER': .25,
'ONE': 1,
'FIVE': 5,
'TEN': 10,
'TWENTY': 20,
'ONE HUNDRED': 100,
};
const cashCount = {};
for (const cash of cid) {
cashCount[cash[0]] = Math.round(cash[1] / cashValues[cash[0]]);
};
let changeAvailable = 0;
for (const cash of cid) {
changeAvailable += cash[1];
};
const cashInput = document.getElementById('cash');
const changeDiv = document.getElementById('change-due');
const purchaseBtn = document.getElementById('purchase-btn');
const calculate = (payment) => {
payment = parseFloat(payment);
const changeAmount = payment - price;
let changeDue = parseFloat((payment - price).toFixed(2));
const cidFlip = cid.slice().reverse();
const cashStack = [];
if (price > payment) {
alert(`Customer does not have enough money to purchase the item`);
} else if (price === payment) {
changeDiv.innerText = `No change due - customer paid with exact cash`;
} else {
for (const cash of cidFlip) {
const cashUnits = Math.floor(changeDue / cashValues[cash[0]]);
const currencyCount = cashCount[cash[0]];
const currencyValue = cashValues[cash[0]];
if (cashUnits >= 1 && currencyCount >= 1) {
const cashAmount = cashUnits <= currencyCount ? cashUnits * currencyValue : currencyCount * currencyValue;
changeDue = ((changeDue - cashAmount).toFixed(2));
if (cashAmount.toString().includes('.') && cashAmount.toString().split('.')[1].length > 2) {
cashStack.push(`${cash[0]}: $${cashAmount.toFixed(2)}`);
} else {
cashStack.push(`${cash[0]}: $${cashAmount}`)
}
};
};
const changeDisplay = cashStack.join(' ');
if (changeAvailable < changeAmount || changeDue > 0) {
changeDiv.innerText = "Status: INSUFFICIENT_FUNDS";
} else if (changeAvailable > changeAmount) {
changeDiv.innerText = `Status: OPEN ${String(changeDisplay)}`;
// test doesn't pass when not explicitly converted to a string,
// couldn't use a variable for the status value
} else if (changeAvailable === changeAmount) {
changeDiv.innerText = `Status: CLOSED ${String(changeDisplay)}`;
}
};
};
const calculateSales = () => {
calculate(cashInput.value);
};
cashInput.addEventListener('keypress', (evt) => {
evt.key === 'Enter' ? calculateSales() : '';
});
purchaseBtn.addEventListener('click', () => {
calculateSales();
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register