const purchaseBtn = document.getElementById("purchase-btn");
const changeShow = document.getElementById("change-due");
const showCurrency = document.getElementById("show-currency");
const totalPrice = document.getElementById("price");
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]
];
const showCurrencyUnit= (array)=>{
const show = array.map((curr)=>{
return `<tr>
<td>${curr[0]}</td>
<td>${curr[1].toFixed(2)}</td>
</tr>`;
}).join("");
showCurrency.innerHTML = show;
totalPrice.innerHTML = price;
}
showCurrencyUnit(cid);
const currencyUnits = [
["ONE HUNDRED", 100],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.1],
["NICKEL", 0.05],
["PENNY", 0.01]
];
// Purchase Btn Click
purchaseBtn.addEventListener('click', function() {
// Get price and cash values from the inputs
const cash = parseFloat(document.getElementById('cash').value);
// Calculate the change due
const changeDue = cash - price;
// Get the total cash in drawer
const totalCid = cid.reduce((total, [, amount]) => total + amount, 0).toFixed(2);
// Check if there are sufficient funds
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
// Check if exact cash was given
if (cash === price) {
document.getElementById('change-due').textContent = "No change due - customer paid with exact cash";
return;
}
// Check if cash in drawer is less than change due
if (parseFloat(totalCid) < changeDue) {
changeShow.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
// Determine the change to be returned
let change = [];
let remainingChange = changeDue;
for (const [unit, value] of currencyUnits) {
let amount = 0;
while (remainingChange >= value && cid.find(([u]) => u === unit)[1] >= value) {
remainingChange = (remainingChange - value).toFixed(2);
amount += value;
cid.find(([u]) => u === unit)[1] -= value;
}
if (amount > 0) {
change.push([unit, amount.toFixed(2)]);
}
}
// Round remainingChange to avoid floating-point precision issues
remainingChange = Number(remainingChange).toFixed(2);
if (remainingChange > 0) {
changeShow.textContent = "Status: INSUFFICIENT_FUNDS";
} else if (parseFloat(totalCid) === remainingChange) {
changeShow.textContent = `Status: CLOSED ${change.map(([unit, amount]) => `${unit}: $${amount}`).join("")}`;
showCurrencyUnit(cid);
} else if (parseFloat(totalCid) > remainingChange) {
changeShow.textContent = `Status: OPEN ${change.map(([unit, amount]) => `${unit}: $${amount}`).join('')}`;
showCurrencyUnit(cid);
}
});
I stopped “Status: Close”. I failed at bottom two tasks. Please help me
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
const purchaseBtn = document.getElementById(“purchase-btn”);
const changeShow = document.getElementById(“change-due”);
const showCurrency = document.getElementById(“show-currency”);
const totalPrice = document.getElementById(“price”);
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]
];
const showCurrencyUnit= (array)=>{
const show = array.map((curr)=>{
return `<tr>
<td>${curr[0]}</td>
<td>${curr[1].toFixed(2)}</td>
</tr>`;
}).join("");
showCurrency.innerHTML = show;
totalPrice.innerHTML = price;
}
showCurrencyUnit(cid);
const currencyUnits = [
[“ONE HUNDRED”, 100],
[“TWENTY”, 20],
[“TEN”, 10],
[“FIVE”, 5],
[“ONE”, 1],
[“QUARTER”, 0.25],
[“DIME”, 0.1],
[“NICKEL”, 0.05],
[“PENNY”, 0.01]
];
// Purchase Btn Click
purchaseBtn.addEventListener(‘click’, function() {
// Get price and cash values from the inputs
const cash = parseFloat(document.getElementById(‘cash’).value);
// Calculate the change due
const changeDue = cash - price;
// Get the total cash in drawer
const totalCid = cid.reduce((total, [, amount]) => total + amount, 0).toFixed(2);
// Check if there are sufficient funds
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
// Check if exact cash was given
if (cash === price) {
document.getElementById('change-due').textContent = "No change due - customer paid with exact cash";
return;
}
// Check if cash in drawer is less than change due
if (parseFloat(totalCid) < changeDue) {
changeShow.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
// Determine the change to be returned
let change = [];
let remainingChange = changeDue;
for (const [unit, value] of currencyUnits) {
let amount = 0;
while (remainingChange >= value && cid.find(([u]) => u === unit)[1] >= value) {
remainingChange = (remainingChange - value).toFixed(2);
amount += value;
cid.find(([u]) => u === unit)[1] -= value;
}
if (amount > 0) {
change.push([unit, amount.toFixed(2)]);
}
}
// Round remainingChange to avoid floating-point precision issues
remainingChange = Number(remainingChange).toFixed(2);
if (remainingChange > 0) {
changeShow.textContent = "Status: INSUFFICIENT_FUNDS";
} else if (parseFloat(totalCid) === remainingChange) {
changeShow.textContent = `Status: CLOSED ${change.map(([unit, amount]) => `${unit}: $${amount}`).join("\n")}`;
showCurrencyUnit(cid);
} else if (parseFloat(totalCid) > remainingChange) {
changeShow.textContent = `Status: OPEN ${change.map(([unit, amount]) => `${unit}: $${amount}`).join('\n')}`;
showCurrencyUnit(cid);
}
});
Please read the post I made earlier about how to format code on the forum. Please update your post so the code is formatted correctly.
when i test your code only the top 6 testcases are passing for me, but you said they all pass for you except the last 2?
Are you sure you have shared the correct code above?