Tell us what’s happening:
Help me with passing the test for this project please))
i cant pass last 2 tests in any cases i cant understand why
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cash Register</title>
</head>
<body>
<main>
<h1>Cash Register Project</h1>
<div id="change-due"></div>
<div class="input-div">
<label for="cash">Enter cash from customer:</label>
<input type="number" id="cash" class="user-input" value="">
<button class="check-btn-styles" id="purchase-btn">Purchase</button>
</div>
</main>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
const cashInput = document.getElementById("cash");
const button = document.getElementById("purchase-btn");
const displayChangeDue = document.getElementById("change-due");
let price = 19.5;
let cid = [
["PENNY", 0.5],
["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0],
];
const checkCashRegister = (price, cash, cid) => {
const currencyUnits = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1.0,
"FIVE": 5.0,
"TEN": 10.0,
"TWENTY": 20.0,
"ONE HUNDRED": 100.0,
};
let changeDue = cash - price;
let totalCid = Number(cid.reduce((sum, element) => sum + element[1], 0).toFixed(2));
if (totalCid < changeDue) return { status: "INSUFFICIENT_FUNDS", change: [] };
if (totalCid === changeDue) return { status: "CLOSED", change: cid };
let changeArr = [];
for (let i = cid.length - 1; i >= 0; i--) {
let currencyUnitName = cid[i][0];
let currencyUnitValueTotal = cid[i][1];
let currencyUnitValue = currencyUnits[currencyUnitName];
let currencyUnitAmount = Number((currencyUnitValueTotal / currencyUnitValue).toFixed(0));
let currencyUnitsToReturn = 0;
while (changeDue >= currencyUnitValue && currencyUnitAmount > 0) {
changeDue -= currencyUnitValue;
changeDue = Number(changeDue.toFixed(2));
currencyUnitAmount--;
currencyUnitsToReturn++;
}
if (currencyUnitsToReturn > 0) {
changeArr.push([currencyUnitName, currencyUnitsToReturn * currencyUnitValue]);
}
}
if (changeDue > 0) return { status: "INSUFFICIENT_FUNDS", change: [] };
return { status: "OPEN", change: changeArr };
};
button.onclick = function () {
const cashValue = Number(cashInput.value);
if (cashValue < price) {
alert("Customer does not have enough money to purchase the item");
} else {
const result = checkCashRegister(price, cashValue, cid);
if (cashValue === price) {
displayChangeDue.innerText = "No change due - customer paid with exact cash";
} else {
displayChangeDue.innerText = `Status: ${result.status} ${result.change.map((item) => item.join(": $")).join(", ")}`;
}
}
};
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register