Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I dont understand how the program should work. On the user stories, on number 6 it says if the price is 19.5 and the value in the cash element is 20 the cid wont change.

On number 8 it says if the price is 19.5 and the value in the cash element is 20 the cid should change in what it says on the user stories.

Also on number 9 and 10 the price is the same and the value in the cash element is the same but the results are supposed to be different. Maybe i missed something but its confusing me.

Can someone explain me how i should calculate the change when the cash element value is larger than price. Also should cid change when the user pays or should it stay the same?

Your code so far

/* file: index.html */
<DOCTYPE html>
<html lang="en">
  <head>
    <title>Cash Register</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="styles.css">
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
  </head>
<body>
  <div id="container">
    <h1 id="title">Cash Register</h1>
    <label id="input-text">Enter amount</label>
    <input id="cash" type="number">
    <button id="purchase-btn">Purchase</button>
    <div id="change-due">
    </div>

    <div id="break"></div>
    
    <div id="cash-in-drawer">
    </div>
  </div>
  <script src="script.js"></script>
</body>
</html>
/* file: script.js */
let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");

let price = 1.87;

let cid = [
  // 101
  ['PENNY', 1.01],
  // 41
  ['NICKEL', 2.05],
  // 31
  ['DIME', 3.1],
  // 17
  ['QUARTER', 4.25],
  // 90
  ['ONE', 90],
  // 11
  ['FIVE', 55],
  // 2
  ['TEN', 20],
  // 3
  ['TWENTY', 60],
  // 1
  ['ONE HUNDRED', 100]
];

function displayDrawer() {
  for(let i = 0; i < cid.length; i++) {
    let el = document.createElement('p');
    el.textContent = `${cid[i][0]}: ${cid[i][1]}`;
    drawer.appendChild(el)
  }
}

btn.addEventListener("click", () => {
  if(cash.value < price){
    alert("Customer does not have enough money to purchase the item");
  }else if(cash.value == price) {
    change.textContent = "No change due - customer paid with exact cash";
    cash.value = "";
  }else if(cash.value > price) {
    for(let i = cid.length; i >= 0; i--){
      for(let j = 0; j < 2; j++){
        
      }
    }
  }
})

displayDrawer();
/* file: styles.css */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

body {
  background-color: #222831;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-content: center;
  flex-wrap: wrap;
}

#container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#title {
  color: #EEEEEE;
  font-family: Poppins;
  font-size: 36px;
  transition: 0.3s;
}

#input-text {
  color: white;
  font-family: Poppins;
  font-size: 18px;
  transition: 0.3s;
}

#cash {
  background-color: #EEEEEE;
  border: none;
  width: 230px;
  height: 48px;
  border-radius: 13px;
  margin: 10px 0 10px 0;
  font-size: 20px;
  text-align: center;
  font-family: Poppins;
  color: #393E46;
  font-weight: 550 ;
  transition: 0.3s;
}

#purchase-btn {
  background-color: #EEEEEE;
  border: none;
  height: 25px;
  width: 100px;
  border-radius: 5px;
  margin: 10px 0 10px 0;
  font-family: Poppins;
  font-weight: bold;
  transition: 0.3s;
}

#change-due {
  font-family: Poppins;
  color: #EEEEEE;
  font-size: 16px;
}

#break {
  width: 340px;
  height: 3px;
  background-color: #EEEEEE;
  border-radius: 25px;
  margin: 20px 0 20px 0;
  transition: 0.3s;
}

#cash-in-drawer {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  flex-direction: column;
}

#cash-in-drawer > p {
  color: white;
  font-family: Poppins;
  margin: 2px;
}


/* Responsive */

#cash:hover, #cash:focus {
  border-radius: 30px;
  outline: none;
}

#purchase-btn:hover {
  background-color: #393E46;
  border-radius: 13px;
  color: white;
}

#break {
  width: 340px;
  height: 3px;
  background-color: #EEEEEE;
  border-radius: 25px;
  margin: 20px 0 20px 0;
  transition: 0.3s;
}

@media screen and (max-width: 355px) {

  #title {
    font-size: 30px;
  }

  #input-text {
    font-size: 16px;
  }

  #cash {
    width: 210px;
    height: 44px;
  }

  #purchase-btn {
    height: 23px;
    width: 95px;
    font-size: 12.5px
  }

  #break {
    width: 280px;
    height: 3px;
  }

}

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

The implementation details have to be yours to come up with because this is a certification project but for now I can explain the idea.

This app is supposed to act like a cash register. Depending on the values of bills and coins available in the register, the app will return change due coins/bills that reflect the total amount due back to the customer who paid in cash.

If the customer pays more than the total amount of money available for change or if the change that is due to them is unavailable in the register (like maybe I owe them one cent but i have no cent coins left to pay as an example), then the register status is insufficient funds. Obviously if i cannot give you change for any reason then the amount of coins in the drawer will not change.

But if the total amount in the register is exactly the same as the amount of change I need to give (such that by the time I give you the change I have no bills or coins left), then the status is CLOSED

Sometimes, depending on what is in the drawer, you will get different values of coins and bills in your change. The totals don’t change but the bills and coins used to pay will change. For eg I may owe you $100 in change. If I have a hundred dollar bill I can just give you that (and by the rules, I should always give you the highest value bill or coin first). But if I am out of hundred dollar bills, then maybe I have ten bills of the ten dollar value that I can give you instead. Or maybe some other combination.

Things to keep in mind for this project:
Don’t remove or modify the two global variables given to you (price and cid) as they are used in the test. You can reassign them to something else to test with but they should remain as cid and price.

Don’t write code in the global scope. Make sure the majority of your functional code is running from the code that is called on the purchase click. You can define some things in the global scope if they do not affect the way the code figures out the change due or the status.

Also one piece of advice. It is easier to just work with cents if you are figuring out the change and values of things because instead of floating point decimal values like $10.01 you could work with 1001 which is a whole number. (Just remember that the output still has to be in dollar values)

1 Like

Ok, I’m taking a look at it rn and see how it goes

Hey can you please give me a hint on how do i count the amount of bills/coins i have used? This is the hardest part imo and i can’t figure it out.

This is what i have done till now:

let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");

let price = 1.87;
// let price = 0.5;

let cid = [
  // 101
  ['PENNY', 1.01],
  // 41
  ['NICKEL', 2.05],
  // 31
  ['DIME', 3.1],
  // 17
  ['QUARTER', 4.25],
  // 90
  ['ONE', 90],
  // 11
  ['FIVE', 55],
  // 2
  ['TEN', 20],
  // 3
  ['TWENTY', 60],
  // 1
  ['ONE HUNDRED', 100]
];

function displayDrawer() {
  for(let i = 0; i < cid.length; i++) {
    let el = document.createElement('p');
    el.textContent = `${cid[i][0]}: ${cid[i][1]}`;
    drawer.appendChild(el)
  }
}

btn.addEventListener("click", () => {
  if(cash.value < price){
    alert("Customer does not have enough money to purchase the item");
  }else if(cash.value == price) {
    change.textContent = "No change due - customer paid with exact cash";
    cash.value = "";
  }else if(cash.value > price) {

    const cashValue = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
    let cashToChange = cash.value - price;
    console.log(cashToChange)

    let i = 0;
    while(cashToChange > 0 && i < cashValue.length){
        if(cashToChange >= cashValue[i]){
          cashToChange -= cashValue[i];
          console.log(cashToChange);
      }else{
        i++;
      }
    }
  }
})

displayDrawer();

How did you solve the decimal to Roman project? Perhaps this is something similar? (Except you’re converting change due to bills and coins)?

On the other hand, how would you give me change in real life while not getting lost? Try to think about that more.

Here is what i came up with:

else if(cash.value > price) {

***let amountOfBills = [1, 3, 2, 11, 90, 17, 31, 41, 101]***

// let oneHundred = 1;
    // let twenty = 3;
    // let ten = 2;
    // let five = 11;
    // let one = 90;
    // let quarter = 17;
    // let dime = 31;
    // let nickel = 41;
    // let penny = 101;

    const cashValue = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
    let cashToChange = cash.value - price;
    // console.log(cashToChange)

    let i = 0;
    ***if(amountOfBills[i] > 0){***
      while(cashToChange > 0 && i < cashValue.length){
        if(cashToChange >= cashValue[i]){
          cashToChange -= cashValue[i];
          ***amountOfBills[i] - 1;***
          console.log(cashToChange);
          console.log(amountOfBills)
        }else{
          i++;
        }
      }
    }
    
  }

The only problem is im trying to figure out why amountOfBills[i] wont be reduced by 1 even though i have written “amountOfBulls[i] - 1”

Edit: I know i have too much comments lol

And yeah, its simmilar to the roman project but im confused on how to count the bills that i have used and make sure that i have the needed ones on the register

I don’t want to hand-hold you because that would defeat the purpose of working on a project like this.

You will need to take enough time to work things out on your own and not be in a rush to solve this one.

Again, I am wondering how you would do this if you were handing out change in real life. If you owed me $34.40 for eg, what would you do first to give me the correct change?

Give you the big bills first and then the small ones

This looks like hardcoding to me.

You should not hardcode the amount of bills. You should figure it out in the code by doing some calculations based on the values in the cid.

If you want to come up with an algorithm you have to be more specific. I gave you a real figure $34.40, what would you really do, in real life, if you had the cash register in front of you?

Edit: Hint

I would start by skipping the $100 dollar bill area and looking the twenty dollar bill area to see if I had any bills there…

I can divide the value on the cid with the value of the coin/bill.

For example: 60/20 = 3 $20 bills

But how can i implement this on code and use it as a condition like i have done here:

if(amountOfBills[i] > 0){

}

I am sorry I didn’t understand?

Most cashiers I know are not doing division.

Here’s how I would start to give you the change of $34.40

I would start by skipping the $100 dollar bill area and looking in the twenty dollar bill area to see if I had any bills there…

Hmm… yeah, im over complicating things. Let me see what i can do.

I did it, the only thing that is left is how to gather all of the change bills on the changeBills[ ] array. I am thinking of subtracting the new totalBillValue[ ] -
the old totalBillValue[ ], but i figured out that i cant since im updating totalBillValue[ ] during the code.

Here is my code:

let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");

// let price = 1.87;
let price = 0.5;

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]
];

function displayDrawer() {
  for(let i = 0; i < cid.length; i++) {
    let el = document.createElement('p');
    el.textContent = `${cid[i][0]}: ${cid[i][1]}`;
    drawer.appendChild(el)
  }
}

btn.addEventListener("click", () => {
  if(cash.value < price){
    alert("Customer does not have enough money to purchase the item");
  }else if(cash.value == price) {
    change.textContent = "No change due - customer paid with exact cash";
    cash.value = "";
  }else if(cash.value > price) {
    let changeBills = [];
    const totalBillValue = cid.map(item => item[1]).reverse();
    // [100, 60, 20, 55, 90, 4.25, 3.1, 2.05, 1.01]
    const billValue = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
    console.log(billValue);
    let cashToChange = cash.value - price;

    let i = 0;
    while(cashToChange > 0 && i < billValue.length){
      if(cashToChange >= billValue[i] && totalBillValue[i] > 0){
        console.log(cashToChange);
        cashToChange -= billValue[i];
        totalBillValue[i] -= billValue[i];
        // Display the bills
      }else{
        i++;
      }
    }
    if(cashToChange > 0){
      alert("Not enough mone");
    }else{
      alert("Transaction completed");
    }
  }
})

displayDrawer();

Hey, i am working on this project but i cant figure out why the “Status CLOSED” wont be displayed.

I found the sum of the cid values and i used an if statement to check if the cash.value === sum.

Here is what i have written so far:

let cash = document.getElementById("cash");
let btn = document.getElementById("purchase-btn");
let change = document.getElementById("change-due");
let drawer = document.getElementById("cash-in-drawer");

let price = 1.87;
// let price = 0.5;

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]
];

function displayDrawer() {
  for(let i = 0; i < cid.length; i++) {
    let el = document.createElement('p');
    el.textContent = `${cid[i][0]}: ${cid[i][1]}`;
    drawer.appendChild(el)
  }
}

let sum = 0;
for(let k = 0; k < cid.length; k++){
  sum += cid[k][1];
}

sum = Math.round(sum * 100) / 100;

console.log(sum);

btn.addEventListener("click", () => {

  change.textContent = "";

  if(cash.value < price){
    alert("Customer does not have enough money to purchase the item");
    cash.value = "";
  }else if(cash.value == price) {
    change.textContent = "No change due - customer paid with exact cash";
    cash.value = "";
  }else if(cash.value === sum){
    change.textContent = "Status: CLOSED";
    cash.value = "";
  }else if(cash.value > price && cash.value !== sum) {
    let changeStatus = document.createElement('p');
    changeStatus.textContent = "Status: OPEN";
    change.appendChild(changeStatus);
    const reversedCid = cid.map(item => item[1]).reverse();
    // [100, 60, 20, 55, 90, 4.25, 3.1, 2.05, 1.01]
    const billValue = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
    let cashToChange = cash.value - price;

    let i = 0;
    while(i < billValue.length){
      if(cashToChange >= billValue[i] && reversedCid[i] > 0){
        cashToChange -= billValue[i];
        reversedCid[i] -= billValue[i];
      }else{
        i++;
      }
    }

    if(cashToChange > 0){
      change.textContent = "Status: INSUFFICIENT_FUNDS"
    }
  }
})

displayDrawer();

you are not asked to change the cid, you are asked to use the cid to calculate the change to give back to the client

cid is different, you can’t give back 0.5 if you have one 0.01 coin and one 1 coin (this is 9)

Yeah, i understand that but what i am struggling with now is this: Build a Cash Register Project - Build a Cash Register - #16 by endispiri4

Did you try logging the variables to the console so you can see if they are what you expect them to be?

The sum is 335.41 but when the input is 335.41 the status will be insufficient funds.

image

One problem that i am encountering now is that even if the cash.value > price the status will show insufficient.

btn.addEventListener("click", () => {

  change.textContent = "";

  if(cash.value < price){
    alert("Customer does not have enough money to purchase the item");
    cash.value = "";
  }else if(cash.value == price) {
    change.textContent = "No change due - customer paid with exact cash";
    cash.value = "";
  }else if(cash.value === sum){
    change.textContent = "Status: CLOSED";
    cash.value = "";
  }else if(cash.value > price) {
    let changeStatus = document.createElement('p');
    changeStatus.textContent = "Status: OPEN";
    change.appendChild(changeStatus);
    const reversedCid = cid.map(item => item[1]).reverse();
    // [100, 60, 20, 55, 90, 4.25, 3.1, 2.05, 1.01]
    const billValue = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
    let cashToChange = cash.value - price;

    let i = 0;
    while(i < billValue.length){
      if(cashToChange >= billValue[i] && reversedCid[i] > 0){
        cashToChange -= billValue[i];
        reversedCid[i] -= billValue[i];
      }else{
        i++;
      }
    }

    if(cashToChange > 0){
      change.textContent = "Status: INSUFFICIENT_FUNDS"
    }
  }
})