*JavaScript Algorithms and Data Structures Projects: Cash Register

Sure, it only passes the tests. I know there is too much “hard code”. For example, in the else if, I know that I would need do it with every denomination.

How would you reccomend me to solve those bugs that I have in my program?

1 Like

you will not return “open” if you can not make exact change

you are not making use of moneyforcustomer variable I told you about it

I correct a little of the spaces you have told me. I will begin to follow the tips you gave me.

use regex replace \n\n with \n
too much space… (or just backspace manually)

the whole first for loop. throw it… use reverse method

you are still multiplying and dividing by large numbers, I said use toFixed

you dont need to declare all the denominations as zero at the beginning

the value of a penny is .01, nickel is .05, dime is .1 etc… not like you have it.

try do these things should help you throw away alot of extra code you have and the rest will become clear.

1 Like

you dont need mutatedcid variable just use cid that is passed as arguments[2]

just use cid

1 Like

I will include these changes, I am thankful to you for your answers, your time, and patience.

1 Like

You’ll get there… look this is the denomination values you can use:

[[ONE HUNDRED", 100],

["TWENTY", 20],



["TEN", 10],



["FIVE", 5],



["ONE", 1],



["QUARTER", .25],



["DIME", .1],



["NICKEL", .05],



["PENNY", .01]

];

1 Like

The thing to remember is how it breaks down it can only be one of the following :

A. What you have equals change you owe
B. What you have is less than what you owe
C. What you have is more than what you owe and you have enough of correct denominations to make exact change
D. What you have is more than what you owe but you don’t have enough correct denominations to make exact change

Solve for it in that order and you’ll see it doesn’t have to be as complex as youve made it. The first two situations you will know right away, the last two you will have to figure out. Put the moneyforcustomer variable in the right place and reset it to zero when you advance to the next compartment

1 Like

wow, your’e stuck on it for like two months, i somehow managed to do it in 1 day kinda first try (spoiler!!! my post). so:
for safety problems, convert every value to cents (*100), you won’t regret it.
first, you need to define a change, which is the cash-price (and you did).

second, define a value array (how many dollars is everything, remember multiplying the values).

third, you need to check the total cash available in your register that will use the values that are under or equal your change (if your change is a quarter but you have one whole dollar you can’t return),define a function for that.

fourth, if your total cash that matches is less than the change u can’t return it, thus return the needed thing (insufficient funds).

fifth, if your total cash is exactly the change, just return CID.

sixth, if your total cash is more than the change, start iterating through CID values (from biggest), if your change is more or equal to the value and your CID is more than the value (if you have 0 at CID you can’t get money from it) then subtract the values from both your change and CID value, to print out the change do CID_before - CID_after and set it to the CID value.

don’t forget to return CID values divided by 100!! (make functions for both the multiplication and division)

hope i helped, if something isn’t clear your’e welcome :slight_smile:

1 Like

it will look something like this:

cash *= 100
price *=100
function multiplyBy100 (arr) {multiply by 100 every CID[i][1]}
multiplyBY100 (CID)
function divideBy100 (arr) {divide by 100 every CID[i][1]}
var change = cash - price;
var total cash = 0
var values = [100*100, 20*100,..., 0.25*100,...]
function totalCashCalc {
for (loop iterates through vals and CID} {
if (change >= values[i]) {totalcash += CID[i][1]}
}
totalCashCalc();
if (totalCash < change) {return INSUFFICIENT FUNDS}
else if (totalCash === change {return divideBy100(CID)}
else {iterate, and that's the hard part, if u don't understand it i'll write it too}

1 Like