Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

12.13.18,19 are failing in the test. But when I am checking them by providing the values that is asked in the instructions, the results are matching. Any ideas how to get around this issue so that I can get my certification ?

Your code so far

<!-- file: index.html -->

/* file: script.js */

/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hi there. Copy and paste your full html, css and js code above in the topic

Hey there,

Please update the message to include your code. The code was too long to be automatically inserted by the help button.

When you enter a code, 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 (').

Unable to edit the main post. Hence adding as a reply:

script.js
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 cid = [
//   ['PENNY', 0.5],
//   ['NICKEL', 0],
//   ['DIME', 0],
//   ['QUARTER', 0],
//   ['ONE', 0],
//   ['FIVE', 0],
//   ['TEN', 0],
//   ['TWENTY', 0],
//   ['ONE HUNDRED', 0]
// ];


let price = 19.5;

const changeDue = document.getElementById("change-due");
const cashGiven = document.getElementById("cash");
const purchaseAmount = document.getElementById("purchase-amount");
const purchaseButton = document.getElementById("purchase-btn");


// For calculating cash in drawer more easily
let cashInDrawer = [
  { name: 'PENNY', value: 0.01,count: 0 },
  { name: 'NICKEL', value: 0.05,count: 0 },
  { name: 'DIME', value: 0.1,count: 0 },
  { name: 'QUARTER', value: 0.25,count: 0 },
  { name: 'ONE', value: 1,count: 0 },
  { name: 'FIVE', value: 5,count: 0 },
  { name: 'TEN', value: 10,count: 0 },
  { name: 'TWENTY', value: 20,count: 0 },
  { name: 'ONE HUNDRED', value: 100,count: 0 }
]

// For storing change to be returned
let returnableChange =  [
  { name: 'PENNY', value: 0.01,count: 0 },
  { name: 'NICKEL', value: 0.05,count: 0 },
  { name: 'DIME', value: 0.1,count: 0 },
  { name: 'QUARTER', value: 0.25,count: 0 },
  { name: 'ONE', value: 1,count: 0 },
  { name: 'FIVE', value: 5,count: 0 },
  { name: 'TEN', value: 10,count: 0 },
  { name: 'TWENTY', value: 20,count: 0 },
  { name: 'ONE HUNDRED', value: 100,count: 0 }
]


// sets up cashInDrawer array with values in cid array
const initCashInDrawer = () => {
  for (let i = 0; i < cid.length; i++) {
    let value = cid[i][1] * 100;
    let denomination = cashInDrawer[i].value * 100;
    let count = Math.floor(value / denomination);
    cashInDrawer[i].count = count;
  }
}

const countCash = (arr)=>{
  let total = 0;
  for (let i=0;i<arr.length;i++){
    total += arr[i].value * arr[i].count * 100;
  }
  return total/100;
}
//Caculate returnable change and display it on change-due div

const calculateReturnableChange = () =>{
  let cash = parseFloat(cashGiven.value) * 100;
  let purchase = price * 100;
  let returnAmount = cash - purchase;
  if(cash===purchase){
    changeDue.innerHTML = "<p>No change due - customer paid with exact cash<p>";
    return true;
  }
  console.log(countCash(cashInDrawer));
  if(countCash(cashInDrawer) < (returnAmount/100)){
    changeDue.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>";
    return true;
  }

 
  for (let i = cashInDrawer.length-1;i>=0;i--){
    while (returnAmount/100 >= cashInDrawer[i].value && cashInDrawer[i].count > 0){
      returnAmount -= cashInDrawer[i].value*100;
      cashInDrawer[i].count--;
      returnableChange[i].count++;
    }
  }
  if (returnAmount === 0){
    let htmlOut = ``;
    for (let i = returnableChange.length-1; i >=0 ; i--){
      if (returnableChange[i].count > 0){
        htmlOut += `<p>${returnableChange[i].name}: $${returnableChange[i].value * returnableChange[i].count}</p>`;
      }
    }
    // console.log(countCash(cashInDrawer), countCash(returnableChange));
    if(countCash(cashInDrawer) === 0){
      htmlOut = "<p>Status: CLOSED</p>" + htmlOut;
    }else{
      htmlOut = "<p>Status: OPEN</p>" + htmlOut;
    }
    changeDue.innerHTML = htmlOut;
    return true;
  } else {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return true;
  }
    return false;
  }


// when Purchase button is clicked
purchaseButton.addEventListener("click",()=>{
  initCashInDrawer();
  purchaseAmount.value = price;
  if(parseFloat(cashGiven.value) < price){
    alert("Customer does not have enough money to purchase the item");
    return false;
  }
  if (!calculateReturnableChange()){
    changeDue.innerHTML = "Sorry, Something went wrong!";
  }
})

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="styles.css" rel="stylesheet">
        <title>Cash Register</title>
    </head>

    <body>
        <div class="wrapper" id="wrapper">
            <header>
                <img src="./images/logo.jpg" alt="logo" class="logo">
            </header>

            <main>
                <h1> Cash Register</h1>
                <div id="cash-register-machine">                    
                    <div id="upper-frame">
                        <div id="input-frame">
                            <div id="cash-div"> 
                                <label for="cash">Cash Given:</label>
                                <input id="cash" type="number" value="0.00" required>
                                
                            </div>
                            <div id="purchase-amount-div">
                                <Label for="purchase-amount">Bill Amount:</Label>
                                <input id="purchase-amount" type="number" value="0.00">                                
                            </div> 
                            <button id="purchase-btn">Calculate</button>
                        </div>
                    </div>
                    <div id="lower-frame">
                        <div id="buttons">
                            <div class="btn">
                                <p>1</p>
                            </div>
                                <div class="btn">
                            <p>2</p>
                                </div>
                            <div class="btn">
                                <p>3</p>
                            </div>
                            <div class="btn">
                                <p>4</p>
                            </div>
                            <div class="btn">
                                <p>5</p>
                            </div>
                            <div class="btn">
                                <p>6</p>
                            </div>
                            <div class="btn">
                                <p>7</p>
                            </div>
                            <div class="btn">
                                <p>8</p>
                            </div>
                            <div class="btn">
                                <p>9</p>
                            </div>
                            <div class="btn">                                
                                <p></p>
                            </div>
                            <div class="btn">
                                <p>0</p>
                            </div>
                            <div class="btn">
                                <p>C</p>
                            </div>
                            
                        </div>                        
                        <div id="change-due"></div>                        
                    </div>                    
                </div>                
            </main>
            <footer>
                <script src="script.js"></script>
                <p class="footer-text">developed by shaunak basu for freeCodeCamp project</p>
            </footer>
        </div>
    </body>
</html>

styles.css

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    color: white;
}

.wrapper{
    width: 425px;
    margin-block-start: 50px;
    margin-inline-start: auto;
    margin-inline-end: auto;
    background-color: #540404;
}

header img{
    display: block;
    margin-inline: auto;
    padding-block: 10px;
}

h1{
    text-align: center;
    color: orange;
    margin-block-end: 10px;
}

label{
    display: inline-block;
    margin-inline-start: auto;
    margin-inline-end: auto;
}

footer{
    display: block;
    text-align: center;
}

footer p{
    padding-block: 10px;
    font-size: 0.7rem;
    color: #adff2f;
}

#cash-register-machine{
    display: grid;
    grid-template-columns: 1fr;
    
}

#upper-frame{
    display: grid;
    grid-template-columns: 1fr;
}

#cash, #purchase-amount{
    width: 4rem;
    text-align: right;
    color: #000000;
}

#cash-div, #purchase-amount-div {
    display: grid;
    grid-template-columns: 1fr 1fr;
    /* justify-content:center;
    align-content:space-around; */
}

#purchase-btn{
    width: 8rem;
    height: 2rem;
    margin: 10px auto;
    /* background-color: #a9a9a9; */
    color: #000000;
    display: block;
    /* border: 1px solid #ffffff; */
    border-radius: 10%;
}

#lower-frame{
    display: grid;
    grid-template-columns: 1fr 3fr;
    margin-inline-start: 50px;
    gap: 10px
}

#buttons{
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
}

.btn{
    width: 3rem;
    height: 2rem;
    background-color: #808080;
    color: black;
    display: grid;
    place-items: center;
    border: 1px solid #ffffff;
    border-radius: 10%;
}

#change-due p {
    font-size: 0.7rem;
    color: white;
    text-align: center;
}

Keep in mind that the tests will call your function many times back to back. Move any global variables such as returnableChange out of global scope and into the function

Thanks for your tip, strangely this solved the issue of all but No. 19. Is there a way to identify for which exact values it is failing ? I have tried a few permutations & combinations and getting the correct output (“Closed” when there is no cash left in drawer).

You would need to share your updated code.

the value in the #change-due element should be "Status: CLOSED"

Maybe it doesn’t like the extra HTML you are adding. Generally, if it’s not in the instructions you should not an an extra <p> element or anything else.

no, it uses different values each time

but you can add a console.log inside your event listener to print the values used

1 Like