Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My code passes all the tests except the last test which doesn’t show the specific values for the test. Can i see the values used in that test?
Note : My code passes the similar tests before that test.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="./styles.css" />
    <title>Cash Register</title>
  </head>
  <body>
    <div id="container">
      <h1>Cash Register Project</h1>
      <div id="change-due">
      </div>
      <div class="input-container">
        <label for="cash">Enter cash from customer:</label>
        <input type="number" id="cash" min="0">
        <button type="submit" id="purchase-btn">Purchase</button>
      </div>
      <div class="price-div">
        <h2 id="price">Total: $</h2>
      </div>
      <div class="drawer">
        <p>Change in drawer:</p>
        <ul>
          <li>Pennies: $<span id="penny"></span></li>
          <li>Nickels: $<span id="nickel"></span></li>
          <li>Dimes: $<span id="dime"></span></li>
          <li>Quarters: $<span id="quarter"></span></li>
          <li>Ones: $<span id="one"></span></li>
          <li>Fives: $<span id="five"></span></li>
          <li>Tens: $<span id="ten"></span></li>
          <li>Twenties: $<span id="twenty"></span></li>
          <li>Hundreds: $<span id="one hundred"></span></li>
        </ul>
      </div>
    </div>
    <script src="./script.js"></script>
  </body>
</html>
/* file: script.js */

let price = 19.5;
let cid = [
  ["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]
];

let units = [["PENNY", .01], ["NICKEL", .05], ["DIME", .1], ["QUARTER", .25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
const button = document.getElementById("purchase-btn");
const priceDoc = document.getElementById("price");
const statusWindow = document.getElementById("change-due");
const cash = document.getElementById("cash");
priceDoc.innerText +=price;


const clearStatus = ()=>{
  statusWindow.innerHTML = '';
}
const updateDrawerWindow=()=>{
  cid.forEach(item=> {
    const moneyItem = document.getElementById(item[0].toLowerCase());
    moneyItem.textContent = item[1];
});
}

const statusUpdate=(changeArr)=>{
  changeArr.forEach(item=>{
    statusWindow.innerHTML += `<p>${item[0]}: $${item[1]}</p>`;
  })
}
const updateDrawer=(changeArr)=>{
  for(let i=0; i<cid.length;i++){
    for(let j=0; j<changeArr.length;j++){
      if(cid[i][0] === changeArr[j][0]){
        cid[i][1] -= changeArr[j][1];
      }
    } 
  }
  updateDrawerWindow();
}
clearStatus();
updateDrawerWindow();

const start = ()=>{
  clearStatus();
  const cashValue = parseFloat(cash.value);
  const changeDue = cashValue - price;

  if(cashValue < price){
    alert("Customer does not have enough money to purchase the item");
    return;
  }
  if(cashValue === price){
    statusWindow.innerHTML += `<p>No change due - customer paid with exact cash</p>`;
    return;
  }
  getChange(changeDue, cid);

}
const getChange = (changeDue, cid)=>{
  let totalOfCid = parseFloat(cid.reduce((sum, [_, amount])=> sum+amount, 0).toFixed(2));
  
  if(totalOfCid < changeDue){
    statusWindow.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }

  let changeArr = [];
  let remainingChange = changeDue;

  for(let i =8; i>=0; i--){
    let unit = units[i][0];
    let unitValue = units[i][1];
    let unitInDrawer = cid[i][1];

    if(unitValue <= remainingChange && unitInDrawer > 0){
      let amounFromUnit = 0;

      while(remainingChange >= unitValue && unitInDrawer > 0){
        remainingChange = (remainingChange - unitValue).toFixed(2);
        unitInDrawer -= unitValue;
        amounFromUnit += unitValue;
      }
      
      if(amounFromUnit >0){
        changeArr.push([unit, amounFromUnit.toFixed(2)]);
      }
    }
  }

  if(remainingChange >0){
    statusWindow.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }
  if (changeDue === totalOfCid){
    statusWindow.innerHTML += `<p>Status: CLOSED</p>`;
    statusUpdate(changeArr);
    updateDrawer(changeArr);
    return;
  }
  statusWindow.innerHTML += `<p>Status: OPEN</p>`;
  statusUpdate(changeArr);
  updateDrawer(changeArr);
  return;
}

button.addEventListener("click", start);


/* file: styles.css */
*,
::before,
::after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body{
  background-color: #1b1b32;
  color: #f5f6f7;
}
#container{
  display : flex;
  align-items: center;
  flex-direction: column;
  margin-top: 40px;
}
h1{
  font-family: Helvetica, Arial, sans-serif;
  font-size: 3rem;
  padding: 30px;
}
#change-due{
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 10px;
  font-size: 1.2rem;
  background-color: #855700;
  color: #f5f6f7;
  width: 18%;
  padding: 5px;
  border-radius: 10px;
  height: auto;
}
.input-container{
  display: flex;
  flex-direction: column;
  font-size: 1.5rem;
  align-items: center;
  padding: 5px;
}
label{
  margin: 5px;
}
input{
  margin: 5px;
  font-size: 1.1rem;
}
button{
  width: 35%;
  font-size: 1.1rem;
  background-color:#ee8319;
  margin: 5px;
  border-radius: 5px;
}
.drawer{
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 10px;
  font-size: 1.2rem;
  background-color: #19ee87;
  width: 18%;
  padding: 5px;
  border-radius: 10px;
}
ul{
  list-style-type: none;
}
li{
  margin: 3px
}
.price-div{
  margin: 10px;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

hello and welcome to fcc forum :slight_smile:

colorful ui, nice :slight_smile:

btw, does your code also meet this condition as well?

When price is less than the value in the #cash element

happy coding :slight_smile:

Hello,
If you look at the project details you can see that the main topic of the project is " When price is less than the value in the #cash element" show how much money will be returned from the register. Also i covered all the edge cases before " When price is less than the value in the #cash element" so at that point only that case stands. And one more thing to point out is in other similar test cases my code passes. Thank you for the reply.

hmm, i figured as such…

calculation wise does it goes with your expectation?

what troubleshooting did you try to find out where its going wrong?

have you tried looking into other topics same as this on forum here?

happy coding :slight_smile:

here’s how you see one of the issues your code is having.
Use this price and cid in your code:

let price = 20.0;
let cid = [
  ["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 1], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]
];

then type 22 in the cash area
this will give INSUFFICIENT_FUNDS which is expected.
without reloading the page (without touching your code), you should then immediately type
25 in the cash area. The result will still be INSUFFICIENT_FUNDS even though we have 1 five dollar bill to use to pay the change.

Give the above a try to confirm. Then add some logs to retry this test and see what is the cause in the code.

Hello and thank you for the reply but the cid array you are suggesting is wrong fundamentally. If you have only one 5 dollars in your cid array cid array should look like this:

cid=[
["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 5], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]
]

So the array you are suggesting is wrong therefore doesn’t answer my question.And if you use the right array you will see that the code performs right.

Also i changed the code a little bit because after repetitive charges the drawer wasn’t showing .2 precise floating number and the changed code is like below please try those codes for future reference.This code also doesn’t pass only the last testcase.
my js code:

let price = 20.0;
let cid = [
  ["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 5], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]
];

let units = [["PENNY", .01], ["NICKEL", .05], ["DIME", .1], ["QUARTER", .25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
const button = document.getElementById("purchase-btn");
const priceSpan = document.getElementById("price-span");
const statusWindow = document.getElementById("change-due");
const cash = document.getElementById("cash");
priceSpan.textContent = price;


const clearStatus = ()=>{
  statusWindow.innerHTML = '';
}
const updateDrawerWindow=()=>{
  cid.forEach(item=> {
    const moneyItem = document.getElementById(item[0].toLowerCase());
    moneyItem.textContent = item[1];
});
}

const statusUpdate=(changeArr)=>{
  changeArr.forEach(item=>{
    statusWindow.innerHTML += `<p>${item[0]}: $${item[1]}</p>`;
  })
}
const updateDrawer=(changeArr)=>{
  for(let i=0; i<cid.length;i++){
    for(let j=0; j<changeArr.length;j++){
      if(cid[i][0] === changeArr[j][0]){
        cid[i][1] -= changeArr[j][1];
        cid[i][1] = parseFloat(cid[i][1].toFixed(2));
      }
    } 
  }
  updateDrawerWindow();
}
clearStatus();
updateDrawerWindow();

const start = ()=>{
  clearStatus();
  const cashValue = parseFloat(cash.value);
  const changeDue = cashValue - price;

  if(cashValue < price){
    alert("Customer does not have enough money to purchase the item");
    return;
  }
  if(cashValue === price){
    statusWindow.innerHTML += `<p>No change due - customer paid with exact cash</p>`;
    return;
  }
  getChange(changeDue, cid);

}
const getChange = (changeDue, cid)=>{
  let totalOfCid = parseFloat(cid.reduce((sum, [_, amount])=> sum+amount, 0).toFixed(2));
  
  if(totalOfCid < changeDue){
    statusWindow.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }

  let changeArr = [];
  let remainingChange = changeDue;

  for(let i =8; i>=0; i--){
    let unit = units[i][0];
    let unitValue = units[i][1];
    let unitInDrawer = cid[i][1];

    if(unitValue <= remainingChange && unitInDrawer > 0){
      let amounFromUnit = 0;

      while(remainingChange >= unitValue && unitInDrawer > 0){
        remainingChange = (remainingChange - unitValue).toFixed(2);
        unitInDrawer -= unitValue;
        amounFromUnit += unitValue;
      }
      
      if(amounFromUnit >0){
        changeArr.push([unit, amounFromUnit.toFixed(2)]);
      }
    }
  }
  if(remainingChange >0){
    statusWindow.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }
  if (changeDue === totalOfCid){
    statusWindow.innerHTML += `<p>Status: CLOSED</p>`;
    statusUpdate(changeArr);
    updateDrawer(changeArr);
    return;
  }
  statusWindow.innerHTML += `<p>Status: OPEN</p>`;
  statusUpdate(changeArr);
  updateDrawer(changeArr);
  return;
}

button.addEventListener("click", start);


my HTML code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="./styles.css" />
    <title>Cash Register</title>
  </head>
  <body>
    <div id="container">
      <h1>Cash Register Project</h1>
      <div id="change-due">
      </div>
      <div class="input-container">
        <label for="cash">Enter cash from customer:</label>
        <input type="number" id="cash" min="0">
        <button type="submit" id="purchase-btn">Purchase</button>
      </div>
      <div class="price-div">
        <h2 id="price">Total: $<span id="price-span"></span></h2>
      </div>
      <div class="drawer">
        <p>Change in drawer:</p>
        <ul>
          <li>Pennies: $<span id="penny"></span></li>
          <li>Nickels: $<span id="nickel"></span></li>
          <li>Dimes: $<span id="dime"></span></li>
          <li>Quarters: $<span id="quarter"></span></li>
          <li>Ones: $<span id="one"></span></li>
          <li>Fives: $<span id="five"></span></li>
          <li>Tens: $<span id="ten"></span></li>
          <li>Twenties: $<span id="twenty"></span></li>
          <li>Hundreds: $<span id="one hundred"></span></li>
        </ul>
      </div>
    </div>
    <script src="./script.js"></script>
  </body>
</html>

CSS code is still the same.

oh sorry about that. I’ll take another look

one more time:
here’s the price and cid I’m using:

let price = 19.5;
let cid = [["PENNY", 0.54], ["NICKEL", 0.1], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

I typed 20 in the cash, and the output was:
Status: OPEN

NICKEL: $0.10

PENNY: $0.40

Which is good. This also leaves .14 in the penny area and 0 everywhere else.
Then without reloading I typed:

19.64

This should have been exactly 14 cents returned but instead I got:
Status: INSUFFICIENT_FUNDS

Hopefully this test is correct now to help you debug.

Thank you for the quick reply. Now i can see a light at the end. Time to debug :smiling_face_with_three_hearts:.

1 Like

It was a .2 decimal debug at the end and i fixed it. I have been looking at this code for some time and was frustrated. Thank you for everything.

1 Like

as a future tip, when dealing with money, always modify to cents at the start so you can deal with whole numbers and then modify back to dollars at the end for the display.

Thank you for the tip. I will remember that. Have a good day hbar1st :relieved:.

1 Like