# Algorithms and Data Structures Projects: Cash Register

Tell us what’s happening:
I’m having trouble thinking of ways on how return the the right amount change. My return is [ [ ‘TWENTY’, 20 ], [ ‘TWENTY’, 20 ], [ ‘TWENTY’, 20 ], [ ‘TWENTY’, 20 ], [ ‘TEN’, 10 ],
[ ‘FIVE’, 5 ], [ ‘ONE’, 1 ], [ ‘QUARTER’, 0.25 ], [ ‘QUARTER’, 0.25 ], [ ‘DIME’, 0.1 ], [ ‘DIME’, 0.1 ],
[ ‘PENNY’, 0.01 ], [ ‘PENNY’, 0.01 ], [ ‘PENNY’, 0.01 ] ]. Instead I’m suppose to return 3 twenties, 2 tens, 15 fives, 1 ones, 2 quarters, 2 dimes, 4 pennies.

I think the computer doesn’t know how much each money is worth??? So, I guess I’m suppose to somehow write a code to tell how much each of them are worth??? I also iterated the “cid” argument but that also returns the wrong change amount. I can’t think of a way of knowing how to do this. Help…

``````
function checkCashRegister(price, cash, cid) {
var money = [['ONE HUNDRED', 100], ['TWENTY', 20], ['TEN', 10], ['FIVE', 5], ['ONE', 1], ['QUARTER', 0.25], ['DIME', 0.10], ['NICKEL', 0.05], ['PENNY', 0.01]];
cid = cid.reverse((a, b) => b - a);
var difference = [price, cash].reduce((a, b) => b - a);
var change = [];
var sumTotal = 0;

cid.forEach(function(val, ind, arr){
sumTotal += arr[ind][1];
});

for(var i = 0; i < money.length; i++){
while(difference > money[i][1]){
change.push([money[i][0], money[i][1]]);
difference -= money[i][1];
}
}

console.log(change)
}

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]]);
``````

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

Before you write any code, make sure you understand what you are supposed to be returning if you were actually going to make change for someone with the amounts shown in the drawer.

Let’s say your starting register drawer looks like:

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

If someone gives you a \$20 bill and you want to give the correct change back starting with highest currency value first, then your code needs to be able to start a the highest value (ONE HUNDRED) and see if you can give the correct change (\$0.50) with an available currency of it. That is not possible, so you then check the next value (TWENTY), and so on until you get to QUARTER, where you do have enough to give the \$0.50 in change back.

You are correct, that your program will need to understand which of these drawer sub-arrays has the highest value.

Hint: You will notice that all the test cases provide the cid with the sub arrays in order of lowest to highest currency value. You can use this information to no where to start checking. Ideally, your solution would be able to provide the correct result regardless of what order the drawer sub arrays are in.

1 Like