# Exact Change - Help

Hi FCC Community!

I’m having an issue with the Front End Developer Advanced Algorithm Scripting Challenge - Exact Change. I believe that I came up with a working solution, but I can’t get it to pass. I know that it’s not exact: my output is [[“QUARTER”,0.5]] instead of [[“QUARTER”, 0.50]], but after hours of trying to get a 0.50 format, I’ve looked at the other threads and other campers have said it’s not necessary to make the output 0.5 vs. 0.50. So now that that’s not the issue, I don’t know how else to debug this. Could someone give me some tips on how to further debug my code? Are there some errors I am not seeing? Thank you!

``````//set values for each currency
var currencyWorth = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.10,
"QUARTER": 0.25,
"ONE": 1.00,
"FIVE": 5.00,
"TEN": 10.00,
"TWENTY": 20.00,
"ONE HUNDRED": 100.00
};

var changeArr = [];

function checkCashRegister(price, cash, cid) {
//calculate change due
var change = cash - price;
change = Math.round(change *100)/100;

//total up cash in drawer
var cashMoney = cid.reduce(function (acc, next) {
return acc + next[1];
}, 0.0);

//check if we don't have enough or if exactly enough.
if (cashMoney < change){
console.log("IF");
return "Insufficient Funds";
} else if (cashMoney == change){
console.log('closed');
return "Closed";
}

var count=0.0;
//iterate through cid amounts. decrement the change by the bill/coin's worth and keep a 'count' for each bill/coin which is pushed onto a result array.
for (i = cid.length - 1; i >= 0; i --){

var moneyType = cid[i][0];
var drawerVal = cid[i][1];

while (change >= currencyWorth[moneyType] && drawerVal >= currencyWorth[moneyType]){

//keep track of current highest bill/coin amount used
count += currencyWorth[moneyType];
count = Math.round(count*100)/100;

//decrement 'change'
change -= currencyWorth[moneyType];
change = Math.round(change*100)/100;

//decrement from cid
drawerVal -= currencyWorth[moneyType];
drawerVal = Math.round(drawerVal*100)/100;

//push to result array once bill/coin is now too high or has been used up
if(change < currencyWorth[moneyType] || drawerVal === 0) {

changeArr.push([moneyType, count]);
count = 0.0;

}

}

}
//if the coins don't match up, there is change left over
if (change > 0){

return 'Insufficient Funds';

}

return changeArr;

}

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

``````

Just move the following line to inside your function checkCashRegister:

``````var changeArr = [];
``````

Global variables are

1 Like

@rmdawson71 Wow, thanks for the help! I don’t understand that at all. Do you have further explanation?

@kylefujisawa - I took your original posted solution with the global variable and added console.log for each test, so you can see results throughout all the tests. See the embedded code below. You can see that changeArr is keeping extra stuff in it because it is global and does not get reset for each test.

1 Like

Awesome! That makes a lot of sense now. Thanks so much for getting back to me! Very helpful!