Tell us what’s happening:
I can not pass the 7th test case when price is 3.26 , the value in the #cash
element is 100
even if my result is exactly the same. Please help.
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" />
<link rel="stylesheet" href="style.css" />
<title>Document</title>
</head>
<body>
<p id="price"></p>
<input id="cash" type="text" />
<button id="purchase-btn">purchase</button>
<div id="change-due"></div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
window.addEventListener("load", (e) => {
const inputCash = document.querySelector("#cash");
const changeDiv = document.querySelector("#change-due");
const purchaseBtn = document.querySelector("#purchase-btn");
purchaseBtn.addEventListener("click", (e) => {
let cash = inputCash.value;
let totalLeft = total(cid);
let change = Math.round((cash - price) * 100) / 100;
let changeArray = giveChange(change);
if (cash < price || cash == "") {
alert("Customer does not have enough money to purchase the item");
} else if (cash == price) {
changeDiv.innerHTML = "No change due - customer paid with exact cash";
} else if (changeArray == false) {
changeDiv.innerHTML = "Status: INSUFFICIENT_FUNDS";
} else if (totalLeft == change) {
changeDiv.innerHTML = `Status: CLOSED ${format(changeArray)}`;
} else if (totalLeft > price) {
changeDiv.innerHTML = `Status: OPEN ${format(changeArray)}`;
}
});
});
let price = 3.26;
let values = {
PENNY: 0.01,
NICKEL: 0.05,
DIME: 0.1,
QUARTER: 0.25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100,
};
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 changeCurrency = [
["ONEHUNDRED", 0],
["TWENTY", 0],
["TEN", 0],
["FIVE", 0],
["ONE", 0],
["QUARTER", 0],
["DIME", 0],
["NICKEL", 0],
["PENNY", 0],
];
function roundNum(num) {
let round = (num.toFixed(3) * 10) / 10;
return round;
}
function total(pocket) {
let total = 0;
for (let i in pocket) {
total += pocket[i][1];
}
return roundNum(total);
}
function giveChange(change) {
let leftChange = roundNum(change);
for (let i in cid.reverse()) {
if (leftChange <= 0) {
return changeCurrency;
}
let value = roundNum(values[cid[i][0]]);
let have = Math.round(cid[i][1] / value);
leftChange = Math.round(leftChange * 100) / 100;
let count = Math.floor(leftChange / value);
let amount = 0;
if (count >= have) {
amount = have;
} else {
amount = count;
}
if (amount >= 1) {
cid[i][1] -= amount * value;
changeCurrency[i][1] += amount * value;
leftChange -= roundNum(amount * value);
}
}
if (leftChange > 0) {
return false;
}
return changeCurrency;
}
function format(array) {
if (array == false) {
return false;
}
let message = "";
for (let i in array) {
let value = array[i][1];
if (value != 0) {
let text = `${array[i][0]}: $${Math.round(value * 100) / 100} `;
message += text;
}
}
return message;
}
// let cash = 20;
// let change = Math.round((cash - price) * 100) / 100;
// console.log(format(giveChange(change)));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register