Tell us what’s happening:
For some reason, i can pass 1 to 13 with the default “cid” but not 14 to 17.
When I change to cid to only get 1 penny, I pass 14 to 17 but not anymore 11 to 13.
Anyone, can help?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Cash Register Project</h1>
<legend for="cash">Enter cash here:
</legend>
<input type="number" id="cash"/>
<button id="purchase-btn">Purchase</button>
<div id="change-due"></div>
<div>
<h1>Price to pay</h1>
<p id="price-pay"></p>
</div>
<div>
<h1>Cash in drawer</2>
<div id="cash-in-drawer">
<ul>
</ul>
</div>
</div>
<script src="./script.js"></script>
</body>
</html>
/* file: styles.css */
/* 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]
];
const ref = [['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.10],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
const cash = document.getElementById("cash");
const changedue = document.getElementById("change-due");
const purchasebtn = document.getElementById("purchase-btn");
document.getElementById("price-pay").innerHTML = `$${price}`;
const drawercash = document.getElementById("cash-in-drawer");
cid.forEach((val) =>
{
drawercash.querySelector('ul').innerHTML += `<li>${val[0]}: $${val[1]}</li>`;
});
let reversecid = [...cid].reverse();
function searchValueInArray (str) {
const row = ref.findIndex(row => row.includes(str));
return ref[row];
}
const calculateChange = (value, cids) => {
let change = [];
let temp = value - price;
cids.forEach((val) => {
let coinType = val[0];
let coinAmount = val[1];
let coinValue = searchValueInArray(coinType)[1];
if (temp >= coinValue && coinAmount > 0) {
let count = 0;
while (temp >= coinValue && coinAmount > 0) {
temp -= coinValue;
coinAmount -= coinValue;
count++;
temp = parseFloat(temp.toFixed(2));
}
if (count > 0) {
change.push([coinType, (count * coinValue).toFixed(2)]);
}
}
});
if (temp > 0.01) {
return "Status: INSUFFICIENT_FUNDS";
}
return change;
}
purchasebtn.addEventListener("click", () =>
{
if(parseFloat(cash.value) < price)
{
alert("Customer does not have enough money to purchase the item");
}
else if(parseFloat(cash.value) === price)
{
changedue.innerHTML = "No change due - customer paid with exact cash";
}
else
{
let string = "";
const result = calculateChange(parseFloat(cash.value), reversecid);
if(Array.isArray(result))
{
string += "<p>Status: OPEN </p>";
result.forEach((val) =>
{
string += `<p>${val[0]}: $${val[1]} </p>`;
});
}
else
string += result;
changedue.innerHTML = string;
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register