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 theprice
andcid
variables. Theprice
variable is the price of the item, and thecid
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.