Hi, I have posted previously but I forgot to include important information. here is the link to the project ( Build a Cash Register Project: Build a Cash Register | freeCodeCamp.org.
Basically my code seems to pass all the tests on the preview. But when I “check code” I do not pass all the tests, only a few I do not pass. So I think it has something to with my JS rather than HTML. I have pasted my JS code below.
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],
['hundred', 100]
];
const arrange = {
hundred: 0,
twenty:0,
ten:0,
five:0,
one:0,
quarter: 0,
dime:0,
nickel: 0,
penny: 0
}
const money = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01]
function Currency({hundred, twenty, ten, five, one, quarter, dime, nickel, penny}){
this.hundred = `HUNDRED: $${Math.round((hundred + Number.EPSILON) * 100) / 100}`;
this.twenty = `TWENTY: $${Math.round((twenty + Number.EPSILON) * 100) / 100}`
this.ten = `TEN: $${Math.round((ten + Number.EPSILON) * 100) / 100}`
this.five = `FIVE: $${Math.round((five + Number.EPSILON) * 100) / 100}`
this.one = `ONE: $${Math.round((one + Number.EPSILON) * 100) / 100}`
this.quarter = `QUARTER: $${Math.round((quarter + Number.EPSILON) * 100) / 100}`
this.dime =`DIME: $${Math.round((dime + Number.EPSILON) * 100) / 100}`
this.nickel = `NICKEL: $${Math.round((nickel + Number.EPSILON) * 100) / 100}`
this.penny = `PENNY: $${Math.round((penny + Number.EPSILON) * 100) / 100}`
}
const changeDue = document.querySelector("#change-due")
const input = document.querySelector("#cash")
const btn = document.querySelector("#purchase-btn")
let cidTotal = 0
for(let i=0; i<cid.length; i++){
cidTotal += cid[i][1]
}
function noMoney(){
if(input.value < price){
alert("Customer does not have enough money to purchase the item")
}else{
return
}
}
let arr = []
let calculate = (change) => {
if(change > 0){
for(let i=0;i<money.length;i++){
for (let j=change; j>0; j = j - money[i])
if(change >= money[i]){
arr.push(money[i])
change -= money[i];
change = Math.round((change + Number.EPSILON) * 100) / 100
}
}
return arr
}
}
function reset (){
input.value = ""
arr=[]
arrange = {
hundred: 0,
twenty:0,
ten:0,
five:0,
one:0,
quarter: 0,
dime:0,
nickel: 0,
penny: 0
}
}
const assign = (array) =>{
if(input.value == price){
changeDue.textContent = "No change due - customer paid with exact cash"
}
else if(cidTotal > input.value-price ||cidTotal === input.value-price ) {
cidTotal === input.value-price ? changeDue.textContent = "Status: CLOSED": changeDue.textContent = "Status: OPEN";
array.map((item) =>{
if(item>=100 && cid[8][1] >= item){
cid[8][1] -= item;
cidTotal -= item
arr.push(item)
arrange.hundred += item;}
else if(item>=20 && cid[7][1] >= item){
cid[7][1] -= item;
cidTotal -= item
arrange.twenty += item;}
else if(item>=10 && cid[6][1] >=item){
cid[6][1] -= item;
cidTotal -= item
arrange.ten += item;}
else if(item>=5 && cid[5][1] >=item){
cid[5][1] -= item;
cidTotal -= item
arrange.five += item;}
else if(item>=1 && cid[4][1] >= item){
cid[4][1] -= item;
cidTotal -= item
arrange.one += item;}
else if (item>=0.25 && cid[3][1] >=item){
cid[3][1] -= item;
cidTotal -= item
arrange.quarter += item;}
else if (item>=0.1 && cid[2][1] >= item){
cid[2][1] -= item;
cidTotal -= item
arrange.dime += item;}
else if (item>=0.05 && cid[1][1] >= item){
cid[1][1] -= item;
cidTotal -= item
arrange.nickel += item;}
else if(item>=0.01 && cid[0][1] >= item){
cid[0][1] -= item;
cidTotal -= item
arrange.penny += item;}
else{
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
reset()
} })}
else if(cidTotal < input.value-price){
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
reset()
}
}
btn.addEventListener("click", () => {
noMoney();
calculate(input.value-price)
assign(arr)
const currency = new Currency(arrange)
function display(){
for(const item in arrange){
if(arrange[item] > 0){
changeDue.innerHTML += ` <div>${currency[item]}</div>`
} } }
display()
reset()
})