Tell us what’s happening:
Some tests are failing but when I manually test with the same values, it outputs exactly as the test expects. I don’t know why it is failing.
Please help.
In the above image, the left is showing that the test failed while the in the right, the manual testing shows the exact output the test expects.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Cash Register</h1>
<input id="cash" />
<button id="purchase-btn">Purchase</button>
<p id="change-due"></p>
</body>
<script src="script.js"></script>
</html>
/* file: styles.css */
/* file: script.js */
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]];
const cash = document.getElementById("cash")
const btn = document.getElementById("purchase-btn")
const due = document.getElementById("change-due")
const units = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100],
]
btn.addEventListener('click',() => {
let change = Number((cash.value - price).toFixed(2))
let indrawer = Number(cid.reduce((a,b) => a+b[1],0).toFixed(2))
if(price > Number(cash.value)) {
alert("Customer does not have enough money to purchase the item")
return;
}
if(price == Number(cash.value)) {
due.innerText = "No change due - customer paid with exact cash"
return;
}
if(change > indrawer) {
due.innerText = "Status: INSUFFICIENT_FUNDS"
return;
}
if(change == indrawer) {
due.innerText = `Status: CLOSED ${cid.filter(x => x[1] !== 0).map(x => `${x[0]}: $${x[1]}`).join(" ")}`
return;
}
cid = cid.reverse()
let coins = units.reverse().map((u,i) => {
let val = 0
while(change >= u[1] && cid[i][1] > 0) {
val += u[1]
cid[i][1] -= u[1]
change -= u[1]
change = change.toFixed(2)
}
return [u[0],Number(val.toFixed(2))]
}).filter(c => c[1] > 0)
if(change > 0) {
due.innerText = "Status: INSUFFICIENT_FUNDS"
return;
}
due.innerText = `Status: OPEN ${coins.map(x => `${x[0]}: $${x[1]}`).join(" ")}`
})
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
Challenge Information:
Build a Cash Register Project - Build a Cash Register