Hello! My code passes the test cases in the sense that it provides the corrects status and change due when the purchase button is clicked, however many of the FCC tests (6, 8, and 9) do not pass.
Any tips very appreciated!
const cash = document.getElementById("cash");
const priceField = document.getElementById("price-field");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDueDiv = document.getElementById("change-due");
const clearBtn = document.getElementById("clear-btn");
let price = 3.26;
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 cid = [
// ["PENNY", 0.5],
// ["NICKEL", 0],
// ["DIME", 0 ],
// ["QUARTER", 0],
// ["ONE", 0],
// ["FIVE", 0],
// ["TEN", 0],
// ["TWENTY", 0],
// ["ONE HUNDRED", 0]
// ];
const currencyArr = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
//let reversedCid = [...cid].reverse(); // shallow copies :(
let reversedCid = JSON.parse(JSON.stringify(cid)).reverse(); // Deep copy (a bit weird, probably more standard way to do this)
// console.log("----")
// console.log(cid)
// console.log(reversedCid)
// cid[0][1] = 100
// console.log("Set to 100")
// console.log(cid)
// console.log(reversedCid)
// console.log("----")
let resultArr = [];
let changeBackArr = [];
priceField.innerText = `Price: ${price}`
// changeDue function
const changeDue = (cash, price) => {
let change = 0;
price = (price * 100);
cash = (cash.value * 100);
change = ((cash - price)/100);
return change; };
// My initial approach to cidSum
const cidSum = (cid) => {
//console.log(cid[0][1]); // reads 0
let sum = 0;
for(let i=0; i<cid.length; i++){
sum += cid[i][1];
}
return sum; };
// // Update cid function
const updateCid = (cash, price) => {
let count = 0;
let change = (changeDue(cash, price))*(100);
reversedCid.forEach((element)=>{
element[1] *= 100; // multiplies $ values by 100
element[1] = Math.ceil(element[1]); // rounds up
})
for (let i = 0; i < reversedCid.length; i++) {
while(change >= currencyArr[i] && change > 0 && reversedCid[i][1] >= currencyArr[i]){
reversedCid[i][1] -= currencyArr[i];
change -= currencyArr[i];
count++;
if(count > 0){
resultArr.push([reversedCid[i][0], currencyArr[i]]);
}
}
} changeBackArr = resultArr.reduce((acc, [label, value]) => {
const existingItem = acc.find(item => item[0] === label);
if (existingItem) {
existingItem[1] += value;
} else {
acc.push([label, value]);
}
return acc;
}, []);
};
// // Register Status Function
const regStatus = (cash,price,cid) =>{
// console.log(cid);
updateCid(cash,price);
// console.log(cid);
let change = changeDue(cash, price);
let sum = (cidSum(cid));
// console.log(sum);
// console.log(changeBackArr);
cash = (cash.value * 100);
price = (price * 100);
if(cash < price){
alert("Customer does not have enough money to purchase the item");
} else if(cash === price){
changeDueDiv.textContent = `No change due - customer paid with exact cash`
} else if(sum < change && change > 0 ){
changeDueDiv.textContent = `Status: INSUFFICIENT_FUNDS`;
} else if(sum === change || (sum-change <= 0)){
changeDueDiv.textContent = `Status: CLOSED`;
for(let i=0; i<changeBackArr.length; i++){
changeDueDiv.textContent += ` ${changeBackArr[i][0]}: $${((changeBackArr[i][1])/100)}`;
}
} else if(cidSum > changeDue){
changeDueDiv.textContent = `Status: OPEN`;
for(let i=0; i<changeBackArr.length; i++){
changeDueDiv.textContent += ` ${changeBackArr[i][0]}: $${((changeBackArr[i][1])/100)}`;
}}
}
// Purchase Button + changeDue Function
purchaseBtn.addEventListener("click", () => {
// add every function here so it is called with "click"
// cidSum(cid);
changeDue(cash, price);
regStatus(cash, price, cid);
});