Project: Build a Cash Register

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() 
  }) 

if the button is pressed multiple times, without restarting the app, does this get reset?

Hi Sorry for late reply, I changed it to let instead of const. It still doesnt work, and now I noticed something even more strange. When I manipulate the values in cid to match the test so I can see if they work and leave the changed cid values. I “run the test” and now more of the tests fail.

That doesn’t make sense to me considering it shouldn’t matter what I put into the cid before running the tests. Why does me changing the cid values effect weather or not the tests past.

because you have things that depend on global values that are not reset between tests. Have in the global scope: the html elements, cid and price. Anything else needs to go in functions that are called with the event listener

I stopped “Status: Closed” task. if(change === totalCid), I checked but It does not work. I failed at bottom two tasks.

hey @thihazaw552000

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

I made many times. I click submit button but my question is not work. so I ask in reply cb.

still, please create your own topic, include your own code and a link to the step, you will get better help than asking in other people topics

Hi Sorry again for late reply, but this project is pretty funny. I changed a bunch of the global variables to be inside a function then run tests, doesnt work, only one wrong. Then I press run test again I pass. I was confused so saved my code and tried again. Did not pass, press the test button a second time I passed. Not sure what is going on but I will take it I guess.

I load the project again, don’t change anything and everytime I press run test there are different number of test that pass and fail.

Some tests use random values and if your code runs into a floating point error, it will fail. If by chance it does not, it passes.