Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Hi!
I have struggled with this challenge but I failed, I have this codes that passes most of the tests except the last two. Unfortunately it doesn’t solve the problem

Your code so far

const cashInput=document.getElementById("cash");
const changeDue=document.getElementById("change-due");
const button=document.getElementById("purchase-btn");
const totalDiv=document.getElementById("total-div");
const pennies=document.getElementById("pennies");
const nickel=document.getElementById("nickels");
const dimes=document.getElementById("dimes");
const quarters=document.getElementById("quarters");
const ones=document.getElementById("ones");
const fives=document.getElementById("fives");
const tens=document.getElementById("tens");
const twenties=document.getElementById("twenties");
const hundreds=document.getElementById("hundreds");
const changePage=document.getElementById("change");
//const myValue=parseFloat(cashInput.value)
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]
];
//displaying default values
pennies.textContent=`$${cid[0][1]}`;
nickel.textContent=`$${cid[1][1]}`;
dimes.textContent=`$${cid[2][1]}`;
quarters.textContent=`$${cid[3][1]}`;
ones.textContent=`$${cid[4][1]}`;
fives.textContent=`$${cid[5][1]}`;
tens.textContent=`$${cid[6][1]}`;
twenties.textContent=`$${cid[7][1]}`;
hundreds.textContent=`$${cid[8][1]}`;
let totalAmount=Math.round(cid.reduce((acc,item)=>acc+item[1],0)*100)/100;
totalDiv.textContent=`Total: ${totalAmount}`;

//change with specific coins
const actualChange=()=>{
  let amountDue=0;
  if(isNaN(parseFloat(cashInput.value))){
    alert("Input is no a valid number");
  }
  else{
     amountDue+=Math.round((parseFloat(cashInput.value)-price)*100)/100;
  if(parseFloat(cashInput.value)<price){
    alert("Customer does not have enough money to purchase the item");
  }
  else if(parseFloat(cashInput.value)===price){
    changeDue.textContent=`No change due - customer paid with exact cash`;
  }
  else if(amountDue===totalAmount){
    const changeAmount=cid.filter((item)=>item[1]===amountDue).flat();
    changeDue.innerHTML=`<p>Status:CLOSED</p> 
    <p>${changeAmount[0]}:$${changeAmount[1]}</p>`;
  }
  else if(amountDue>totalAmount){
    changeDue.textContent=`Status: INSUFFICIENT_FUNDS`;
  }
  else{
    //handle correct amount of change
    let change={};
      change.status="OPEN";
      change.change=[];
const amounts={
  "ONE HUNDRED":100,
  TWENTY:20,
  TEN:10,
  FIVE:5,
  ONE:1,
  QUARTER:0.25,
  DIME:0.1,
  NICKEL:0.05,
  PENNY:0.01
}
Object.keys(amounts).forEach((key)=>{
if(amountDue>amounts[key]){
  let amountOfBillInCid=0;
  cid.forEach((type)=>{
    if(type[0]===key){
     amountOfBillInCid= type[1];
    }
  })
  let amountDueBefore=amountDue;
  amountDue=amountDue%amounts[key];
  amountDue=Math.round(amountDue*100)/100;
  let difference=Math.round((amountDueBefore-amountDue)*100)/100;
  if(difference>amountOfBillInCid){
    change.change.push([key,amountOfBillInCid]);
    amountDue=amountDueBefore-amountOfBillInCid;
  }
  else{
     change.change.push([key,difference]);
  }
}
});
changeDue.textContent=`Status: ${change.status}`;
change.change.forEach((item)=>{
  changeDue.insertAdjacentHTML("beforeend", `<p>${item[0]}: $${item[1]}</p>`)
});
    }
    if(amountDue>0){
      changeDue.textContent=`Status: INSUFFICIENT_FUNDS`;
    }
    }
    }//actualChange();
    
button.addEventListener("click",actualChange);
<!-- file: index.html -->

/* file: script.js */

/* file: styles.css */

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

Hi @qumalo2021 nice to see you back.

I am afraid there are few things in your code that could be done better.

We will try to guide you along. We will also try to go step by step, problem by problem. Looking at your code it appears that there are few concepts that might require further clarification and the revision of previous theory, but we will know more along the thread.

First of all, give us a help: can you please show us your html too? It appears that you modified the template provided by the exercise?


Also, please keep an eye on code readability in the forum posts:

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Here is the HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cash Register</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
   <main class="container">
    <nav class="nav-bar">
    <img src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg"
     alt="freecodecamp logo" class="nav-img">
    </nav>
    <h1 class="header text-white large-text">Cash Register Project</h1>
    <div class="input-container">
        <div id="change-due"></div>
    <form>
        <fieldset>
            <legend class="text-centered">Enter cash from customer:</legend>
            <div class="change" id="change"></div>
    <p class="input text-centered">
        <input type="text" id="cash" required>
    </p>
        <button class="button text-centered" id="purchase-btn" type="button">Purchase</button>
    </fieldset>
    </form>
    <div class="total-div text-centered" id="total-div"></div>
    <div class="small"></div>
    <div class="content-div">
        <div class="container-child">
            <div class="left-child">
                <div class="holder">
                    <div class="square"></div>
                    <div class="square"></div>
                    <div class="square"></div>
                </div>
                <div class="holder">
                    <div class="square"></div>
                    <div class="square"></div>
                    <div class="square"></div>
                    </div>
                    <div class="holder">
                    <div class="square"></div>
                    <div class="square"></div>
                    <div class="square"></div>
                </div>
                </div>
                </div> 
    <div class="container-child">
        <div class="right-child">
           <p>Pennies: 
            <span id="pennies"></span>
           </p>
           <p>Nickels: 
            <span id="nickels"></span>
           </p>
           <p>Dimes: 
            <span id="dimes"></span>
           </p>
           <p>Quarters: 
            <span id="quarters"></span>
           </p>
           <p>Ones: 
            <span id="ones"></span>
           </p>
           <p>Fives:
            <span id="fives"></span>
           </p>
           <p>Tens:
            <span id="tens"></span>
           </p>
           <p>Twenties: 
            <span id="twenties"></span>
           </p>
           <p>Hundreds: 
            <span id="hundreds"></span>
           </p>
        </div>
    </div>
</div>
        
<div class="footer">
    <div class="circle"></div>
</div>
</div>
   </main>
   <script src="index.js"></script>
</body>
</html>

this is one line that may cause problems with the testcases.
It is defined in the global scope and will not get reset between tests.
This means that whatever the totalAmount was at the start of the first test that runs, it will stay unchanged as other tests run without reloading your code (only the purchase event is triggered by the tests in sequence).

Move the code that calculates totalAmount into the same callback function that triggers on a purchase and see if that changes your results.

Thank you, let me try

1 Like

When I put that line of code inside the function, it doesn’t run at all


Only these two tests could not pass

Not sure what you mean by “it does not run”?

I mean that when I run the codes, it does nothing

And have you tried to debug?
Have you looked at the console to see if you have a error?

I am going to try again

Please do. We are happy to help guide you to come up with something that works through your own effort.

What is wrong with link to the project https://cash-register.freecodecamp.rocks it is always denied by antivirus

it works fine for me. Maybe you can open it on another browser that doesn’t have the virus checker you are using.