Hi, I’m having problems with this end project. I know the final projects are meant to be personal challenges, I wouldn’t ask for help if I wasn’t desperate.
I did some testing and my code seems to work fine but when I run the final tests there are two points that fail. It’s odd because when I manually modify the values of “cid” and “price” then I see the expected results inside the paragraph element.
These are the two points that I fail:
When price
is 19.5
, the value in the #cash
element is 20
, cid
is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
, and the #purchase-btn
element is clicked, the value in the #change-due
element should be "Status: CLOSED PENNY: $0.5"
.
When price
is less than the value in the #cash
element, total cash in drawer cid
is equal to change due, and the #purchase-btn
element is clicked, the value in the #change-due
element should be "Status: CLOSED"
with change due in coins and bills sorted in highest to lowest order.
I should say that I experienced some strange behaviour when running the final tests. Sometimes I only change the values inside “cid” for the purpose of testing, one the two points succeeds but a different one fails.
And here’s my code.
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]
];
let totalMoney = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]).toFixed(2)
const cash = document.getElementById("cash")
const purchase = document.getElementById("purchase-btn")
const change_due = document.getElementById("change-due")
const change = difference => {
let hundred = ""
let twenty = ""
let ten = ""
let five = ""
let one = ""
let quarter = ""
let dime = ""
let nickel = ""
let penny = ""
let sum = 0
for (let i = 1; i <= cid[8][1] / 100; i++) {
if (difference >= 100) {
difference -= 100;
difference = difference.toFixed(2)
sum += 100;
hundred = `ONE HUNDRED: $${Number((100*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[7][1] / 20; i++) {
if (difference >= 20) {
difference -= 20;
difference = difference.toFixed(2)
sum += 20;
twenty = `TWENTY: $${Number((20*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[6][1] / 10; i++) {
if (difference >= 10) {
difference -= 10;
difference = difference.toFixed(2)
sum += 10;
ten = `TEN: $${Number((10*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[5][1] / 5; i++) {
if (difference >= 5) {
difference -= 5;
difference = difference.toFixed(2)
sum += 5;
five = `FIVE: $${Number((5*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[4][1] / 1; i++) {
if (difference >= 1) {
difference -= 1;
difference = difference.toFixed(2)
sum += 1;
one = `ONE: $${Number((1*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[3][1] / 0.25; i++) {
if (difference >= 0.25) {
difference -= 0.25;
difference = difference.toFixed(2)
sum += 0.25;
quarter = `QUARTER: $${Number((0.25*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[2][1] / 0.1; i++) {
if (difference >= 0.1) {
difference -= 0.1;
difference = difference.toFixed(2)
sum += 0.1;
dime = `DIME: $${Number((0.1*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[1][1] / 0.05; i++) {
if (difference >= 0.05) {
difference -= 0.05;
difference = difference.toFixed(2)
sum += 0.05;
nickel = `NICKEL: $${Number((0.05*i).toFixed(2))} `;
}
}
for (let i = 1; i <= cid[0][1] / 0.01; i++) {
if (difference >= 0.01) {
difference -= 0.01;
difference = difference.toFixed(2)
sum += 0.01;
penny = `PENNY: $${Number((0.01*i).toFixed(2))}`;
}
}
let change = hundred.concat(twenty, ten, five, one, quarter, dime, nickel, penny)
if (difference > 0.00) {
change_due.innerText = "Status: INSUFFICIENT_FUNDS"
return
}
else if (Number(sum.toFixed(2)) === Number(totalMoney)) {
change_due.innerText = `Status: CLOSED ${change}`
return
}
else if (difference == 0.00) {
change_due.innerText = `Status: OPEN ${change}`
return
}
}
const buy = () => {
if (Number(cash.value) < Number(price)) {
alert("Customer does not have enough money to purchase the item");
change_due.innerText = "";
}
else if (Number(cash.value) === Number(price)) {
change_due.innerText = "No change due - customer paid with exact cash";
}
else {
change_due.innerText = "";
let difference = Number(cash.value) - Number(price)
change(difference)
}
}
purchase.addEventListener("click", buy)