# Question about the Exact Change assignment

The code I’m using below works for all test cases except the test case involving Price: 3.26 and Cash: 100:

``````// Array that stores the coefficients for the money
var coefficients = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];
var change = [];
var exact = false;
var balance = cash - price;
var amount = 0;
var closed = false;
console.log("Price: " + price + " Cash: " + cash);
console.log(cid);

for(var i = cid.length - 1; i >= 0; i--) {
balance *= 100;
var inflatedcid = cid[i][1] * 100;
console.log("Inflated CID for " + cid[i][0] + " is: " + inflatedcid);
amount = Math.floor(balance/coefficients[i]);
if(amount*coefficients[i] <= inflatedcid && amount !== 0) {
balance -= amount*coefficients[i];
change.push([cid[i][0], amount * coefficients[i] / 100]);
if(amount*coefficients[i] === inflatedcid) {
exact = true;
}
}
console.log("Balance: " + balance);
if (exact === true) {
return "Closed";
}
else if (balance === 0) {
return change;
}
balance /= 100;
}
if(balance > 0) {
return "Insufficient Funds";
}
``````

When I use the code above for the test case that doesn’t work as intended I get the output “Insufficient Funds” which implies that I don’t have enough cash to make change. I think the issue is that my code above only processes the request if the cash in drawer available is greater than that required to make the change. The accompanying array for cash in drawer is:
[[“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]]

I tried using this code to deal with the situation:

``````  else if(amount*1 > inflatedcid && inflatedcid !== 0 && amount !== 0) {
balance -= inflatedcid;
}
``````

Where inflatedcid is the cash in drawer times 100 to avoid floating point errors. This doesn’t work as intended and causes changes when I don’t want the balance to be affected. I just can’t get my head around the algebra required to test for the situations where the amount of cash available is less than that required but can still be allocated for correct change combined with other cash in the drawer from other denominations.

Sorry for the complex post, I’m really close to finishing this I just need some help figuring out the formula.

Well one thing I notice… You might want to rethink your array structure for your return method… Or ‘strictly’ speaking, you ‘luck’ out’ that the just previously gives you a ‘pass’ for:

return [[“QUARTER”, 0.50]].

If you console log your ‘change’ array just before you ‘return it’ you will see:

So part of your issue on the test that you mention is you are still returning quantity of currencies that you do not use in making the correct amount of change.

For example, because you never ‘remove’ that array element from your list, on the test in question above you will notice you are still returning ‘One Hundred’, 100.

That cash however is still in the drawer not part of the returned change. It is only the returned change that we are asked to produce from the function.

Correction – Was looking at this quickly-- There is also a logic issue as you say, but of course not removing CID from array will of course still cause you problems.

1 Like

Alright, I’ll clean up the array first. Thanks.

Fixed the code, apparently there was still a floating point error and I fixed some of the logic/variables so it gave me the right answer.