My codes does not pass although it produces the correct results and I cannot figure out why. My JS code:
function modulo(a, b) {
if (b === 0) {
throw new Error("Division by zero is undefined");
}
let result = a - Math.floor(a / b) * b;
// Handle negative result to ensure correct modulo
return result < 0 ? result + b : result;
}
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 cidAmount = [
['PENNY', 0],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
let totalInCid = 0;
const amount = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
for(let i=0; i<cidAmount.length; i++){
cidAmount[i][1] = Math.round(cid[i][1] / amount[i]);
totalInCid = parseFloat((totalInCid + cid[i][1]).toFixed(2));
}
console.log(cidAmount)
//console.log(totalInCid) 335.41
const input = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const messageText = document.getElementById("change-due");
//console.log("Int-Division: ",Math.floor( 96.74 / 20))
//console.log("Remainder: ", 96.74 % 20)
purchaseBtn.addEventListener("click", ()=>{
if(input.value === ""){
alert("Please enter a value!")
return;
} else {
let cash = parseFloat(input.value);
if(cash < price){
alert("Customer does not have enough money to purchase the item");
return;
} else if(cash === price){
messageText.textContent = "No change due - customer paid with exact cash";
return;
} else {
let change = cash - price;
if(change > totalInCid){
messageText.textContent = "Status: INSUFFICIENT_FUNDS";
return;
} else {
let changeArrAmount = [
['PENNY', 0],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
let intDivision = 0;
for(let i=cid.length-1; i>=0; i--){
intDivision = Math.floor(change / amount[i]);
if (cidAmount[i][1] < intDivision){
let difference = intDivision - cidAmount[i][1];
changeArrAmount[i][1] = cidAmount[i][1];
change = modulo(change, amount[i]) + (difference * amount[i]);
change = change.toFixed(2);
cidAmount[i][1] = 0;
cid[i][1] = 0;
} else {
changeArrAmount[i][1] = intDivision;
change = modulo(change, amount[i]);
change = change.toFixed(2);
cidAmount[i][1] -= intDivision;
cid[i][1] = cid[i][1] - (intDivision * amount[i]);
}
}
change = Number(change);
if(change !== 0){
messageText.textContent = "Status: INSUFFICIENT_FUNDS";
return;
}
totalInCid = 0;
for(let i=0; i<cidAmount.length; i++){
cidAmount[i][1] = Math.round(cid[i][1] / amount[i]);
totalInCid = parseFloat((totalInCid + cid[i][1]).toFixed(2));
}
let outputText = ";"
console.log(totalInCid)
if(totalInCid === 0){
outputText = "Status: CLOSED "
for(let i=changeArrAmount.length -1; i>=0; i--){
if(changeArrAmount[i][1] === 0){
continue;
} else {
outputText += `${changeArrAmount[i][0]}: $${(changeArrAmount[i][1] * amount[i])} `
}
}
messageText.textContent = outputText;
return;
}
//console.log(cidAmount)
outputText = "Status: OPEN "
for(let i=changeArrAmount.length -1; i>=0; i--){
if(changeArrAmount[i][1] === 0){
continue;
} else {
outputText += `${changeArrAmount[i][0]}: $${(changeArrAmount[i][1] * amount[i])} `
}
}
messageText.textContent = outputText;
}
}
}
})
My HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<title></title>
</head>
<body>
<main>
<input id="cash"/>
<button id="purchase-btn" type="button">Purchase</button>
<div id="change-due">Placeholder</div>
<script src="./script.js">
</script>
</main>
</body>
</html>