Tell us what’s happening:
My code works, but the FCC tests don’t accept it. I believe it’s an error on your side. How can this be fixed? I ran all the tests manually changing the price, cid and cash values and it’s all perfect. I did use the search bar and there are apparently other people who got this issue but unfortunately I do not understand what the solution was. First, I thought it’s because I was working with integers as cents so I adapted it to use floats but STILL it doesn’t pass, while manually it’s all flawless.
This is my JS:
let price = 11.95;
let actualPrice = Math.round(price * 100);
let cid = [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.10],
["QUARTER", 4.25],
["ONE", 90.00],
["FIVE", 55.00],
["TEN", 20.00],
["TWENTY", 80.00],
["ONE HUNDRED", 200.00]
];
let actualCid = cid.map(([name, value]) => [name, Math.round(value * 100)]);
console.log(actualCid);
let due = [
["PENNY", 0],
["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0]
];
let regStatus = 'open';
let noChange = false;
//get elements
const cashInput = document.getElementById('cash');
const changeDueDiv = document.getElementById('change-due');
const itemPriceSpan = document.getElementById('item-price');
const purchaseBtn = document.getElementById('purchase-btn');
const statusSpan = document.getElementById('status');
const noChangeParagraph = document.getElementById('no-change');
const [...cidSpans] = document.querySelectorAll('.cid-span');
const [...dueSpans] = document.querySelectorAll('.due-span');
//onstart
updateDisplay();
//buttons
purchaseBtn.addEventListener('click', () => {
purchase();
updateDisplay();
changeDueDiv.classList.remove('hidden');
});
//functions
function purchase() {
const cash = Math.floor(parseFloat(cashInput.value) * 100);
const change = (cash - actualPrice);
const totalCid = actualCid.map(el => el[1]).reduce((a, b) => a + b);
if (regStatus !== 'CLOSED') {
if (cash >= actualPrice) {
if (cash > actualPrice) {
if (totalCid >= change) {
noChange = false;
processChange(change);
updateDisplay();
} else {
noChange = false;
regStatus = 'INSUFFICIENT_FUNDS';
updateDisplay();
}
} else {
noChange = true;
updateDisplay();
}
} else {
alert('Customer does not have enough money to purchase the item');
}
} else return;
}
function processChange(cdue) {
let work = cdue;
//100
const hundreds = calculateAmount(10000, 8, work);
due[8][1] = hundreds;
work -= hundreds;
//20
const twenties = calculateAmount(2000, 7, work);
due[7][1] = twenties;
work -= twenties;
//10
const tens = calculateAmount(1000, 6, work);
due[6][1] = tens;
work -= tens;
//5
const fives = calculateAmount(500, 5, work);
due[5][1] = fives;
work -= fives;
//1
const ones = calculateAmount(100, 4, work);
due[4][1] = ones;
work -= ones;
//quarters
const quarters = calculateAmount(25, 3, work);
due[3][1] = quarters;
work -= quarters;
//dimes
const dimes = calculateAmount(10, 2, work);
due[2][1] = dimes;
work -= dimes;
//nickels
const nickels = calculateAmount(5, 1, work);
due[1][1] = nickels;
work -= nickels;
//pennies
const pennies = work;
due[0][1] = pennies;
if (actualCid[0][1] >= work) {
actualCid.forEach((el, index) => {
el[1] -= due[index][1];
});
const totalCid = actualCid.map(el => el[1]).reduce((a, b) => a + b);
regStatus = totalCid > 0 ? 'OPEN' : 'CLOSED';
} else {
regStatus = 'INSUFFICIENT_FUNDS';
return;
}
}
function substLimitZero(a, b) {
return a - b > 0 ? a - b : 0;
}
function calculateAmount(value, cidIndex, working) {
return (Math.floor(working / value) - substLimitZero(Math.floor(working / value), actualCid[cidIndex][1] / value)) * value;
}
function twoDecimalDisplay(num) {
return (num / 100).toString();
}
function updateDisplay() {
itemPriceSpan.textContent = twoDecimalDisplay(actualPrice);
cidSpans.forEach((span, index) => {
span.textContent = twoDecimalDisplay(actualCid[index][1]);
});
if (!noChange) {
if (regStatus === 'OPEN' || regStatus === 'CLOSED') {
noChangeParagraph.classList.add('hidden');
statusSpan.parentElement.classList.remove('hidden');
statusSpan.textContent = regStatus;
dueSpans.forEach((span, index) => {
span.textContent = twoDecimalDisplay(due[8 - index][1]);
const dueParagraph = span.parentElement;
if (due[8 - index][1] <= 0) {
dueParagraph.classList.add('hidden');
} else {
dueParagraph.classList.remove('hidden')
}
});
} else if (regStatus === 'INSUFFICIENT_FUNDS') {
noChangeParagraph.classList.add('hidden');
statusSpan.parentElement.classList.remove('hidden');
statusSpan.textContent = regStatus;
dueSpans.forEach(span => {
span.parentElement.classList.add('hidden');
});
}
} else {
noChangeParagraph.classList.remove('hidden');
statusSpan.parentElement.classList.add('hidden');
dueSpans.forEach(span => {
span.parentElement.classList.add('hidden');
});
}
}