Build a Cash Register Project - Build a Cash Register

Thank you for your time. i will go through it and drop update here

send cid to console.log() at the end of your function and watch what happens to it as you do your testing

i send the cid to the console and when i press the purchase button the cid appears with the bills from highest denomination to lowest and if i press purchase button again it appears with the cid from lowest to highest. i tried decrementing totalAmountInDrawer but that didn’t work

    while (change >= equivalentOfAmountInDrawer && availableAmount > 0) {
        change = Number((change - equivalentOfAmountInDrawer).toFixed(2));
        totalAmountInDrawer--;
        availableAmount--;
        amountCollected++;
      }

you may want to avoid changing the global cid

but don’t i need the cid.reverse() to reverse the array so it could start from the highest denomination in the array?

but what does reverse do?
it changes cid, it does not create a new copy

so you are changing the global cid

Now you see why your output keep reversing back and forth?

No, you don’t need reverse() to do that. Think about a simple for loop to access array elements in reverse.

Also, think about checking each currency that has a value in cid rather than just the total amount in the drawer. I mean, if you have to give change of 5.38 and you have no fives and you have only 3 ones and 2 quarters and no dimes or nickels and just 30 pennies, you can’t give change. Your code doesn’t recognize that.

ok i created a variable that will handle reversing the cid array without changing it. i did const reversedArray = cid.slice().reverse(); and then i used the forEach method on the newly reversedArray variable reversedArray.forEach() that seem to work. now i have to figure out how to make the code check for each currency that has value

i can’t get pass step 16-19. I’m trying to get the bills that were used to give change to the customer to decrement but it seems i’m getting that wrong. the cid array doesn’t run out after giving change even tho i tried subtracting the amountCollected from the available amount if (amountCollected > 0) { const amount = Number((amountCollected * equivalentOfAmountInDrawer).toFixed(2)); changeArr.push([` ${coinName}: ` + `$${amount}` ]); availableAmount -= amountCollected; } i tried different approaches but i don’t just get it right. I really need help

The change in the drawer is kept in the cid variable, correct?

Yes it is and the amountAvailable is holding the elements in the index 0 of the cid array

Why is that? Isn’t the cid the amount of change available?

yes it is. But i used a variable totalAmountInDrawer (not amountAvailable sorry it was a mistake) to get the elements that are in the index 0 of the cid const reversedArray = cid.slice().reverse(); reversedArray.forEach((el) => { let coinName = el[0]; let totalAmountInDrawer = Number(el[1]); let equivalentOfAmountInDrawer = Number(currencyUnit[coinName]); let availableAmount = Number((totalAmountInDrawer / equivalentOfAmountInDrawer).toFixed(2)); let amountCollected = 0; after returning change i subtracted amountCollected from the totalAmoountinDrawer

From the instructions:

In the script.js file, you have been provided with the price and cid variables. The price variable is the price of the item, and the cid variable is the cash-in-drawer, which is a 2D array listing the available currency in the cash drawer.

The cid variable is the cash-in-drawer. If you are not changing the cid variable then the cash in the drawer is not changing.

1 Like

changing the cid as how i dont quite understand. I did this

if (amountCollected > 0) {
amountCollected = Number((amountCollected * equivalentOfAmountInDrawer).toFixed(2));
changeArr.push([` ${coinName}: ` + `$${amountCollected}` ]);
totalAmountInDrawer -= amountCollected;
console.log(totalAmountInDrawer);
}

I logged totalAmountInDrawer in the console and it return the expected amount of bill that will remain in the cid after giving change. But it doesn’t reflect in the main cid the amount remain thesame even tho the console shows whats remaining of the bill that was used. I’m actually consfuse rn

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, 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 (').

1 Like

I don’t see in your code where you update cid? If cid is not referenced I’m not sure why you think it would change at all.

You also need to keep track of each denomination, not just an overall total

When your own code confuses you, it’s time to add comments to describe what you are doing and log your variables so you can see what’s happening each step of the way. You may even want to rename some of your variables so it’s easier to understand what they represent.

// display price and contents of cash drawer
// user clicks the purchase button
// console.log(“in event listener”);
// check for cash value & handle no cash given
// handle when cash given is less than price
// handle when cash given is equal to price
// console.log(“in calculateChange”)
// get the total cash in drawer and change due
… etc.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.