# Cash Register- stuck

Tell us what’s happening:
So, I’m kinda losing my mind here. My ideas was to look at amount in the drawer and compare it to change starting from the highest amount, so like change is 60 and 100 has one bill while 20 has 4. it would see 100 can’t be used but i can do twenty, and the array that returned would have [‘TWENTY’, 60]. I don’t know I just need some advice I guess, the for loop is supposed to find the amount and push that into the array. It
s frustrating because I have an idea how what I need to do but i’m struggling to implement it.

``````function checkCashRegister(price, cash, cid) {
let availableMoney = 0;
let currency = [ [ 'ONE-HUNDRED DOLLARS', 100 ],
[ 'TWENTY DOLLARS', 20 ],
[ 'TEN DOLLARS', 10 ],
[ 'FIVE DOLLARS', 5 ],
[ 'DOLLAR', 1 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'NICKEL', 0.05 ],
[ 'PENNY', 0.01 ] ];

let change = cash - price;
//Supposed to see if the cid is greater than change while also making sure the cash value works with change amount e.g. no 20's for \$15 and returns the bill amount
function cidTotal(cash, cDrawer){
let tot = 0;
if (cDrawer === change){
return cDrawer;
}else if (cDrawer >= change){
do {
tot = cash[1]++;
} while(tot <= change)
}
}
const cidCopy = cid.splice(cid);
const reverse = cidCopy.reverse();
let arr = [];
for (let money = 0; money < reverse.length; money++){
console.log(reverse[money])
if (reverse[money][1] >= change){
//supposed to make a new array with the amount per bill for change
let val = (cidTotal(currency[money], reverse[money][1]))
arr.push(reverse[money][0],val);
//console.log(availableMoney);
} else{
availableMoney = 0;

}

}
console.log(arr.reverse());
//console.log(availableMoney)
if (availableMoney < change){
return {status:"INSUFFICIENT_FUNDS", change: []};
} else if (availableMoney == change){
return {status: "CLOSED", change: cid};
}

}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0`

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

You’re on the right track with your thinking but the code doesn’t line up in a few places. For example, availableMoney is always 0 so this always returns `INSUFFICIENT_FUNDS` unless the change is also 0.

Also, the output isn’t in the right format; you need to return an array of arrays, which you can do by adding brackets to your push like this:

``````arr.push([reverse[money][0], val]);
``````

It seems that after that you just have a few changes to make in the way you determine how much of each denomination to dispense.

This is a tough project because it’s hard to keep track of all the array operations you have to do, but I can tell that you know the right process in your head.

I recommend you write down the steps from start to finish without writing code so you know exactly what you should be typing. Then, console.log checkCashRegister with the different test cases and trace the arguments through your code, keeping track of how the variables change.

If something doesn’t make sense along the way, look back at the steps you wrote and think about what the code should be doing at that moment in time. Maybe renaming variables more explicitly could help, too. It’s all about the process, good luck!!

Thanks for the advice. I feel like its helped, I’ll be sure to come back when I hopefully finish it soon. Sometimes it helps just to get some reassurance and issues pointed out.

So i’ve been working on it and I think I’m almost there but i am struggling now. I feel like I’m close and I decide to just get the values right before doing anything else but looking at this test case for total change being like \$96.74 it goes well but for some reason doesn’t add the fives to 15 even though the change is 16. it stops at 10 and adds the rest which still is the correct amount of change but idk what is going wrong here. Sorry to bother you again any help is appreciated.

``````function checkCashRegister(price, cash, cid) {
let availableMoney = 0;
let currency = [ [ 'ONE-HUNDRED DOLLARS', 100 ],
[ 'TWENTY DOLLARS', 20 ],
[ 'TEN DOLLARS', 10 ],
[ 'FIVE DOLLARS', 5 ],
[ 'DOLLAR', 1 ],
[ 'QUARTER', 0.25 ],
[ 'DIME', 0.1 ],
[ 'NICKEL', 0.05 ],
[ 'PENNY', 0.01 ] ];

let change = cash - price;
//Supposed to see if the cid is greater than change while also making sure the cash value works with change amount e.g. no 20's for \$15 and returns the bill amount
function changeBack(cash, cDrawer){
let tot = 0;
let tempChange =  change;
if (cDrawer === tempChange){
return cDrawer;
}else if (cash < tempChange){
do {

tot += cash;

tempChange -= cash;

} while(tot <= tempChange && cash <= tempChange)
}

}
let cidCopy = cid.splice(cid);
let reverse = cidCopy.reverse();
let arr = [];
for (let money = 0; money < reverse.length; money++){

let val = changeBack(currency[money][1], reverse[money][1])

if (val <= change){
arr.push([reverse[money][0], val]);
change -= val;
}
console.log(change);
}
console.log(arr);

//console.log(availableMoney)
if (availableMoney < change){
return {status:"INSUFFICIENT_FUNDS", change: []};
} else if (availableMoney == change){
return {status: "CLOSED", change: cid};
}

}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) ```
``````

Take a look at the `do while` in the `changeBack`, add some print outs to check what values have `tot`, `tempChange`, etc.

I figured out my issue, thanks. It seems that having do while tot was <= change was a the issue since with the fives the total would go up to 10 even though there was 6 left but since ten is greater than 6 it moved to the next bill. Unfortunately i am still struggling to figure out how to fix it. Maybe making a new variable? I don’t really know haha. Thank you though for the advice. Also apologies if you already had a reply from me, I didn’t see your picture in the upper right corner.

Try writing it in plain words - what that condition is trying to accomplish. One part is making sure change will not go under `0`, what’s the other one?

I’m not quite sure what you mean. If you mean the while conditional then I need change to not go below zero and the cash in drawer needs to be greater than or equal to zero, as in using up to the last bill necessary . The weird thing is though it doesn’t go all the way to zero. and when I look at the values getting printed they have a ton of extra decimal places like I’ll get 16.739999999999995 when it gets there. Anyway at least how I understand it I need to subtract one currency value of change every loop and taking one currency from the drawer each loop. This is my updated loop

``````function changeBack(cash, cDrawer){
let tot = 0;

//let tempChange =  change;

if (cash <= change && change >= 0){
//console.log(change);
do {
console.log(cDrawer);
cDrawer = cDrawer - cash;
//console.log(cDrawer);
tot += cash;

change -= cash;

//console.log(tot);

} while(change > 0 && cDrawer > 0 && cash <= change);
}
} ```
``````

The issue is this is what the values look like

``````60
40
20
20
10
55
50
45
90
4.25
4
3.1
3
1.01
1
0.99
[ [ 'TWENTY', 60 ],
[ 'TEN', 20 ],
[ 'FIVE', 15 ],
[ 'ONE', 1 ],
[ 'QUARTER', 0.5 ],
[ 'DIME', 0.2 ],
[ 'PENNY', 0.03 ] ]
``````

the top values being the change amount and bottom is the array that comes from changeback. I would like to thank you again for your time and apologize if I’m just not getting it.

You have a decimal number of dollars but an integer number of cents. Why not use cents?

I got it for the most part now at least. I made change be (change * 100)/ 100. and same with the tot. Now I just need to figure out the insufficient funds one. I’m thinking of going with a map to see if any of the values can be used and if not return insufficient funds.

Ehh, you are still working with a decimal there. Safer/simpler to stay in cents until the end usually.

We do have “friendly” test cases here - if the cash in the drawer is large enough, then you will be able to make change.

I’m still not quite sure what you mean by staying in cents. Should I make it a float? Also the insufficient funds one is giving me more trouble than I thought haha I was all excited since those two were the only test cases left.

Multiplying by 100 and then immediately dividing by 100 keeps your values a decimal numbers, and decimal numbers have rounding. You can instead keep whole numbers if you multiply by 100 and don’t divide by 100 until the very end.

I finished the cert haha! But I’m still a little confused, would I be multiplying everything by 100? Like currency amount, cash in drawer, change and total and then just dividing the returned total by 100? Sorry if I’m frustrating you and thanks for taking your time to explain this to me.

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