Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I made all this, and now im stuck, i dont know how to write for, how to search, what, and how much to subsctract from input.value, or should i subsctract from it at all?I dont know what do i even do, i feel so ass about my skills

Your code so far

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

/* file: script.js */
const status = document.getElementById('status');
const input = document.getElementById('cash');
const output = document.getElementById('change-due');
const purchaseBtn = document.getElementById('purchase-btn');
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]
];
const denominations = [
    ['ONE HUNDRED', 100],
    ['TWENTY', 20],
    ['TEN', 10],
    ['FIVE', 5],
    ['ONE', 1],
    ['QUARTER', 0.25],
    ['DIME', 0.1],
    ['NICKEL', 0.05],
    ['PENNY', 0.01]
  ];
const checkResults = () => {
    const cash = parseFloat(input.value);
    const changeNeeded = (cash - price).toFixed(2);
    
    if (cash < price) {
      alert("Customer does not have enough money");
      return;
    }
    
    if (cash === price) {
      output.textContent = "No change needed";
      return;
    }

    let remainingChange = changeNeeded;
    const changeBreakdown = [];
    
    const tempCid = [...cid].reverse();

    for (const [name, value] of denominations) {
      if(input.value >= value){
         input.value -= value;
         
      }
    }
}


purchaseBtn.addEventListener("click", checkResults)
/* 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/130.0.0.0 Safari/537.36 OPR/115.0.0.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

If you were handed a bunch of money and had to hand me a certain amount from that pile of money, how would you do that by hand?

Depending on amount, search what u need and give it.Or what do you mean?

I’m not sure what “search what u need” means. Can you be more specific?

if u wanted 50$, i would search for it in my pocket for example and give it to you

But what if I wanted something that cannot be given as a single bill. Say if I wanted $30.52?

give you 30, 0,50, 0,02

The US does not have 30 dollar bills and this project does not have 50 cent pieces.

These are the denominations that we have. How would you give me $30.52?

3 tens 2 quaters 2 pennies

That would work. Why did you start with $10 dollar bills?

oh 1 twenty 1 ten 2 quaters and 2 pennies

Ok, that’s sounding almost like an algorithm. Why did you start with the $20 dollar bill?

because its less then the amount before, if i gave u the amount before, it would be too big

Brilliant, that is starting to sound like a process we can formalize with some if statements and loops.

After you pick out a $20 to give to me, you would have to still find $10.52, right? So how would you know to pick a $10 bill next?

because now 20 is too big, but i dont get, how do i get to it, if the number before is too big, do i write forEach and do index + 1?

I wouldn’t worry about any fancy loop methods just yet.

99% of the time, you can do what you need just with if statements and plain old for loops.

so like, what do i do?

for(const [name, value] of denominations){
 if(value <= change){
change -= value;
}
}

Or do i write while?

That’s not a plain old for loop, but that works too.

Plain old for loop:

for (let i = 0; i < denominations.length; i++) {
}

But yeah, some flavor of for loop over the denominations sounds right to me.

Where are you thinking you might need a while?

i think i might need it like in converter we made earlier, but i dont know, we subsctracted value of object while we could, then went to the next object, and so long, until 0