Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I am so confused and i’ve been stuck for almost a week now, i dont even know where to start for the isChangeAvailable function, i’ve went through others code but it’s not helping out i don’t understand i’m losing it right now. Please i need help on how to start this. Thanks in advance

Your code so far

<!-- 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/136.0.0.0 Safari/537.36 Edg/136.0.0.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Please don’t try to replicate other people’s code. You should not read solutions until you have your own working solution.

Please post what code you have tried so far.

I’ve posted the code but its not showing

Please post it again. The code is not in your post.

const changeDue = document.getElementById("change-due");
const input = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const cashInDrawer = document.querySelectorAll("cid");
const innerContent = document.getElementById("cid-content");
const total = document.getElementById("total");

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]
];
let currencyUnit = {
  'PENNY': 0.01,
  'NICKEL': 0.05,
  'DIME': 0.1,
  'QUARTER': 0.25,
  'ONE': 1,
  'FIVE': 5,
  'TEN': 10,
  'TWENTY': 20,
  'ONE HUNDRED': 100
};

total.innerHTML = `<h2>Total: $${price}</h2>`;

innerContent.innerHTML = `<h3>Change in Drawer:</h3>
<div class="cid-content">Pennies: $${cid[0][1]}</div>
<div class="cid-content">Nickels: $${cid[1][1]}</div>
<div class="cid-content">Dimes: $${cid[2][1]}</div>
<div class="cid-content">Quarters: $${cid[3][1]}</div>
<div class="cid-content">Ones: $${cid[4][1]}</div>
<div class="cid-content">Fives: $${cid[5][1]}</div>
<div class="cid-content">Tens: $${cid[6][1]}</div>
<div class="cid-content">Twenties: $${cid[7][1]}</div>
<div class="cid-content">Hundreds: $${cid[8][1]}</div>`;

const isChangeAvailable = (price, cash) => {
  
}

purchaseBtn.addEventListener("click", () => {
  let userInput = parseFloat(input.value);
  
  if (userInput < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  } else if (userInput === price) {
    changeDue.innerHTML = "No change due - customer paid with exact cash";
    return "";
  } else {
    isChangeAvailable(price, cash);
  }
})

I’m stuck on the isChangeAvailable function i dont even know what to do

How would you count out change if you were doing this as a human?

I will calculate the total amount of the items bought and then subtract it from the amount of cash the person give me

Ok,s so in code how could you determine how much change you need to make if you know the price and cash?

Once you have that amount of change that you need to make, how would you go about creating that change from a cash drawer?

I will search through the cash drawer and take out the change that i will give

Be more specific please. Computers need very small steps to follow.

i will subtract the price from the cash whatever amount that is remaining i will then search through the drawer to pick out the amount remaining

How do you search through the drawer? How do you pick out the amount you need?

i will loop through the drawer array and maybe create another array that will store the new amount i picked?

You the human being loop through a drawer array? I don’t know what that means.

How do you, a physical human being, look at a physical cash drawer and pull out a specific amount of money that adds up to an amount you need?

I will check for the amount i need in the cash drawer

How?

Please provide specific tiny step by tiny step details.

ok let say the total price of the item is $13.25 and the customer gave me $20 i will look for $5 bring it out then $1 and add it up to the 5 then $0.25 then another $0.25 and another $0.25 and add it all together
thats the total of $6.25

That’s starting to sound like a process a computer can use.

How did you know to start with $5?

How did you know to only use 1 $5?

How did you know not to use a $0.10 dime?

because $5 is the only currency unit almost close to the the change i’m looking for and if i use it twice then it will exceed the amount i am supposed to give so i only need one and i can’t use 0.10 dime because i have something that will be much better which is 0.25 because its bigger than 0.10