This is my code:
const drawer = document.getElementById("drawer-container");
const purchase = document.getElementById("purchase-btn");
const input = document.getElementById("cash");
const output = document.getElementById("change-due");
let isAllZero = 0;
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]
];
/*let cid =[["PENNY", 0.41], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 5], ["FIVE", 0], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]*/
const money = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', 0.25 ],
['ONE', 1 ],
['FIVE', 5 ],
['TEN', 10 ],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
let moneyVar = []
money.forEach((cash)=>{
moneyVar.push(cash[1])
})
moneyVar.reverse()
//console.log(moneyVar)
let cidVar = []
cid.forEach((cash)=>{
cidVar.push(cash[1])
})
cidVar.reverse()
//console.log(cidVar)
purchase.addEventListener("click", ()=>{
output.innerText = "";
if(input.value == price){
output.innerText = "Status: CLOSED";
output.innerHTML = `<p>No change due - customer paid with exact cash</p>`
}else if(input.value < price){
alert(`Customer does not have enough money to purchase the item`)
//output.innerText = "Status: INSUFFICIENT_FUNDS";
}else{
calculate(input.value);
}
})
//console.log(5.65-1.87)
const calculate = (input)=>{
let target = (Math.round((input-price)*100)/100)
let sum = 0;
let reset = [];
cidVar.forEach((cash)=>{
reset.push(cash)
});
let change = [];
let j=0;
//console.log(target)
if(sum != target){
for(let i=0; i<moneyVar.length; i++){
if(moneyVar[i] < target){
while(sum < target && Math.round((sum+moneyVar[i])*100)/100 <= target && Math.round((cidVar[i]-moneyVar[i])*100)/100 >= 0){
change[j] = moneyVar[i]
j++;
sum += moneyVar[i]
sum = (Math.round(sum*100)/100)
cidVar[i] = cidVar[i]-moneyVar[i];
cidVar[i] = (Math.round(cidVar[i]*100)/100)
}
}
}
}
isAllZero = cidVar.every(item => item === 0);
if(isAllZero && sum != target){
cidVar = reset;
sum = 0;
}
if(!isAllZero && sum != target){
cidVar = reset;
sum = 0;
}
//console.log(isAllZero)
//console.log(cidVar)
cidVar.reverse()
cid.forEach((cash, index)=>{
cash[1] = cidVar[index]
})
update();
cidVar.reverse()
checkStatus()
//console.log(sum)
//console.log(cid)
function checkStatus (){
if(isAllZero && sum == target){
output.innerText = "Status: CLOSED";
updateChange(change)
}
if(!isAllZero && sum != target){
output.innerText += "Status: INSUFFICIENT_FUNDS";
}
if(!isAllZero && sum == target){
//console.log(change)
output.innerText += "Status: OPEN";
updateChange(change);
}
if(isAllZero && sum != target){
output.innerText += "Status: INSUFFICIENT_FUNDS";
}
}
}
const updateChange = (arr) =>{
const changes = {}
arr.forEach(cash=>{
if(!changes[cash]){
changes[cash] = 1;
}else{
changes[cash]++
}
})
let amount=[];
let type=[];
money.forEach((cash,index)=>{
if(changes[cash[1]]){
let product = changes[cash[1]]*cash[1]
product = (Math.round(product*100)/100)
amount.push(product);
type.push(cash[0]);
}
})
amount.reverse()
type.reverse()
//console.log(amount)
//console.log(type)
amount.forEach((cash, index)=>{
output.innerHTML +=`<p>${type[index]}: $${cash}</p>`;
})
}
const update =() =>{
drawer.innerHTML = `<p>Change in drawer: </p>`;
cid.forEach((cash)=>{
drawer.innerHTML += `<p>${cash[0]}: $${cash[1]}</p>`;
})
}
drawer.innerHTML = `<p>Change in drawer: </p>`;
cid.forEach((cash)=>{
drawer.innerHTML += `<p>${cash[0]}: $${cash[1]}</p>`;
})
P.S - The console.logs were there for me to debug. Sorry for the unorganized code
Issues: 1) last seven test cases don’t work when I run the code. But if i try it out manually it works fine.
2) Also my projects works fine exactly like the one in the example project
3) I have even used chatgpt to solve this problem and it actually worked but i dont feel comfortable with that solution because i actually want to learn what is it that im doing wrong and I want to solve the problem all by myself
4) At this point I have wasted so many times to solve this project, that I have actually given up.
What I want?
I want help from the pros to test my code out with various test cases and let me know for what kind of test cases my code doesn’t work. I feel like i am never gonna be able to solve this problem :[