# Struggling with "Exact Change"

Hi. I’ve been really struggling with the “Exact Change” challenge all day. I think I’m not even close, but I’m having so much trouble figuring out why my loops aren’t and recursion isn’t working.

``````function checkCashRegister(price, cash, cid) {
var remaining = cash-price;
var values = {
"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 change = [];
var totalRegister = 0;
for (var i in cid) {
totalRegister+=cid[i][1];
}

console.log("NEW ARRAY");
console.log(cid);
function function1(remaining) {
if (remaining>0) {
console.log("success1");
if (remaining>totalRegister) {
console.log("Insufficient Funds");
return "Insufficient Funds";
} else {
for (var j=cid.length-1;j>0;j--) {
console.log(j);
var tempBillVal = 0.00;
if (cid[j] == undefined) {
console.log("continued");
continue;
} else if (remaining>values[cid[j][1]]) {
if (remaining>values[cid[j][1]] && cid[j][1]<0) {
console.log("Insufficient Funds 2");
return "Insufficient Funds";
}
while (remaining>values[cid[j][1]] && cid[j][1]>0) {
console.log([cid[j][1]]);
cid[j][1]-=values[cid[j][1]];
remaining-=values[cid[j][1]];
tempBillVal+=values[cid[j][1]];
console.log("actually worked bill");
} change.push([cid[j][0] + ": " + tempBillVal]);
console.log("pushed " + [cid[j][0] + ": " + tempBillVal]);
if (remaining>0) {
console.log("return function1(remaining)");
return function1(remaining);
} else {
console.log("return change");
return change;
}

}
}
}
}
}
// Here is your change, ma'am.
return function1(remaining);
}

// Example cash-in-drawer array:
// [["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]]

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

If you have written out your algorithm (not code) in plain language and worked through the test cases to validate your algorithm logic works on paper, then it is just a matter of transcribing your written algorithm to JavaScript.

Did you write out an algorithm first or just starting typing out code without a clear direction of how the finished solution would work? If not, it might be time to take a step back and make sure you even have an algorithm which will generate the correct response in each test case.