Tell us what’s happening:
Can you tell me why I can’t pass these tests? I don’t see problem.
Your code so far
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fcc Cash Register Project</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main>
<h1 id="h1-main">Welcome to our Shopping mall</h1>
<div id="change-due"></div>
<div class="input-div">
<label for="cash" id="label">Enter cash from customer:</label>
<input id="cash" type="number" min="0">
<button id="purchase-btn">Purchase</button>
</div>
<div class="container">
<div>
<div id="top">
<p id="total">Total: $1.87</p>
</div>
<div id ="connector"></div>
</div>
<div id="top-register">
<div id="buttons-container">
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
<button class="btn"></button>
</div>
<div id="true">
<p id="fcc-market">fcc Market</p>
<p id="myName">Fardin Mohammadi</p>
</div>
<div id="cash-info"></div>
</div>
</div>
</main>
<script rel="stylesheet"src="script.js"></script>
</body>
</html>
let input = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let changeDiv = document.getElementById("change-due");
let cashInfo = document.getElementById("cash-info");
let ko = []
let item = {}
let status;
let bazmande = [];
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 c1d = [
100,
20,
10,
5,
1,
0.25,
0.1,
0.05,
0.01,
];
let c1dName = [
'ONE HUNDRED',
'TWENTY',
'TEN',
'FIVE',
'ONE',
'QUARTER',
'DIME',
'NICKEL',
'PENNY'
]
let numberss = [];
// window.onload = () => {
// }
class ShoppingCart {
constructor(x, y) {
// this.total = 0
this.name = x;
this.price = y;
// this.object = {};
// this.item = {}
}
addItem(x, y) {
if (this.name == x) {
// changeDiv.innerHTML = "Status: OPEN"
if (!document.getElementById(this.name)) {
const na = document.createElement("p");
na.textContent = `${this.name}: $${y}`;
na.id = this.name;
changeDiv.appendChild(na);
let lo = document.getElementById(this.name + this.name);
this.price =parseFloat((this.price - y).toFixed(2));
lo.textContent= `${this.name} $${this.price.toFixed(2)}`
} else {
let na = document.getElementById(this.name);
let lolo = document.getElementById(this.name + this.name);
// console.log(item[y])
this.price = parseFloat((this.price - y).toFixed(2));
y = parseFloat((y * item[y]));
// console.log(this.price)
lolo.textContent= `${this.name} $${this.price.toFixed(2)}`
// console.log(lolo.textContent)
na.textContent = `${this.name}: $${y}`;
// console.log(lolo.textContent)
}
}
}
}
const prices = cid.map((_name, index, names) =>
new ShoppingCart(names[index][0], names[index][1]));
const update = () => {
prices.forEach(({name, price}) => {
const lo = document.createElement("p");
lo.id = name + name;
lo.className = "lo"
lo.textContent = `${name}: $${price}`
cashInfo.appendChild(lo)
// console.log(lo)
})
}
update();
const createElement = (content) => {
let lo = document.createElement("p");
lo.textContent = content;
changeDiv.appendChild(lo);
lo.id = "changeDivv"
}
// if (remaining > totalAvailableChange) {
// const changeDivv = document.getElementById("changeDivv")
// changeDivv.textContent = "Status: INSUFFICIENT_FUNDS";
// return
// }
function updateArray(arrays) {
console.log(arrays)
arrays.forEach((_number, index, numbers) => {
numberss.push(numbers[index][1]);
})
}
function toBeRunOnce(args) {
// some stuff to do
updateArray(args)
console.log('toBeRunOnce has completed');
toBeRunOnce = function() {};
}
const addItem = (value) => {
let remaining = value;
// let arrays = [...cid];
let denominations = [...c1d];
let denominationNames = [...c1dName];
createElement("Status: OPEN");
toBeRunOnce(cid);
while (remaining > 0) {
// Check if the remaining value is greater than the sum of all available denominations
const totalAvailableChange = parseFloat(numberss.reduce((sum, denomination) => sum + denomination, 0).toFixed(2));
console.log(totalAvailableChange)
if (remaining > totalAvailableChange) {
const changeDivv = document.getElementById("changeDivv")
changeDivv.textContent = "Status: INSUFFICIENT_FUNDS";
// console.log(totalAvailableChange)
return
}
else if (totalAvailableChange == remaining) {
changeDivv.textContent = "Status: CLOSED";
}
// Find the closest denomination that is less than or equal to the remaining value
const closestDenominationIndex = denominations.findIndex((denomination) => denomination <= remaining);
// If no denomination is found, break out of the loop
if (closestDenominationIndex === -1) {
break;
}
const closestDenomination = denominations[closestDenominationIndex];
const denominationName = denominationNames[closestDenominationIndex];
const pricec = prices.find(({ name }) => name === denominationName);
const { price } = pricec;
// If the price is 0, remove the denomination from the arrays
if (price === 0) {
denominations.splice(closestDenominationIndex, 1);
const removedElement = denominationNames.splice(closestDenominationIndex, 1)[0];
const indexToRemoveFromArrays = Math.abs(cid.length - c1dName.indexOf(removedElement) - 1);
console.log(indexToRemoveFromArrays);
if (indexToRemoveFromArrays >= 0) {
const lop = cid.splice(indexToRemoveFromArrays, 1);
// console.log(lop);
const lopcompleted= cid.filter((name) => name !== lop);
// console.log(arrays)
numberss = [];
updateArray(lopcompleted)
} else {
numberss = [];
updateArray(cid)
}
// console.log(arrays)
continue;
}
// If the price is greater than or equal to the remaining value
if (!price - remaining < 0) {
item[closestDenomination] = (item[closestDenomination] || 0) + 1;
prices.forEach((price) => price.addItem(denominationName, closestDenomination));
ko.push(closestDenomination);
remaining = parseFloat((remaining - closestDenomination).toFixed(2));
} else {
// If the price is less than the remaining value, move to the next denomination
denominations.splice(closestDenominationIndex, 1);
denominationNames.splice(closestDenominationIndex, 1);
}
}
return rabet();
};
const rabet = () => {
// numberss = []
ko = [];
item = {};
}
const conditions = () => {
while (changeDiv.hasChildNodes()) {
changeDiv.removeChild(changeDiv.firstChild);
}
if (!input.value) {
changeDiv.innerHTML = `<p class="jsP">You forgot your wallet?</p>`
}else if (input.value < price) {
alert("Customer does not have enough money to purchase the item.");
changeDiv.innerHTML = ""
}else if (input.value == price) {
createElement("Status: CLOSED");
changeDiv.innerHTML = "No change due - customer paid with exact cash";
}else{
input.value-= price;
addItem(parseFloat(input.value).toFixed(2));
// if (input.value > 0) {
// }
}
input.value = ""
}
btn.addEventListener("click", conditions);
input.addEventListener("keydown", (e)=> {
if(e.key === "Enter") {
conditions();
// console.log(cid)
}
})
*, ::before, ::after{
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--containerBack-color: #BC1700
}
body {
background-color: #00A5BC;
}
main {
text-align: center;
margin-top: 35px;
font-style: oblique;
}
#h1-main {
margin-bottom: 18px;
font-weight: 800;
font-family: "Lato", Helvetica, Arial, sans-serif;
}
.jsP {
/* color: #BC00A5; */
color: #FF1500;
margin-bottom: 14px;
font-size: 19px
}
.input-div {
display: flex;
flex-direction: column;
/* justify-content: center; */
align-items: center;
gap: 10px;
margin-bottom: 65px;
}
#label {
font-size: 20px;
}
input {
width: 220px;
height: 25px;
padding-left: 8px;
}
#purchase-btn {
cursor: pointer;
padding: 4.55px;
background-color: #47BC00;
border-radius: 4px;
border: 1px solid #A5BC00;
margin-top: -2px;
font-weight: bold;
letter-spacing: 0.6px;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-left: 20px;
}
#top {
width: 120px;
height: 37px;
background-color: var(--containerBack-color); /* #A5BC00 */
color: white;
justify-content: center;
align-items: center;
display: flex;
flex-wrap: wrap;
text-align: center;
border-radius: 3px;
/* position: relative; */
margin-left: -130px;
}
#total {
width: 80%;
height: 70%;
background-color: black;
margin: 0 auto;
color: white;
text-align: center;
justify-content: center;
align-items: center;
display: flex;
border-radius: 3px;
}
#connector {
width: 30px;
height: 20px;
background-color: var(--containerBack-color);
/* position: absolute; */
/* left: 0 */
/* top: 0; */
margin-top: -2px;
margin-left: -90px;
border-radius: 1px;
}
#top-register {
/* width: 41%; */
width: 310px;
height: 250px;
background-color: var(--containerBack-color);
display: flex;
position: relative;
/* justify-content: space-around; */
}
#buttons-container {
width: 27%;
margin: 15px 4px;
}
.btn {
width: 20px;
height: 20px;
background-color: #47BC00;
border: none;
}
/* #for-fun {
position: absolute;
/* bottom: 150px; */
/* } */
#true {
width: 90px;
height: 79px;
background-color: black;
position: absolute;
/* color: white; */
bottom: 40px;
border-radius: 5px;
display: flex;
flex-direction: column;
justify-content: space-around;
left: 7.5px;
/* top: 0; */
}
#fcc-market {
font: bold 13px Comic Sans MS, Comic Sans, cursive;
color: #d2ae2d
}
#myName {
width: 80px;
font: bold 14.5px Comic Sans MS, Comic Sans, cursive;
font-style: oblique;
color: #80a758
}
#cash-info {
width: 45%;
height: 90%;
background-color: #b5dcf2;
border-radius: 12px 25px;
justify-content: center;
align-items: center;
margin: auto;
}
#cash-info {
display: flex;
flex-wrap: wrap;
flex-direction: column;
/* align-items: center; */
justify-content: center;
gap: 4.5px
}
/* .lo { */
/* align-items: center;
justify-content: center; */
/*
} */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register