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.

