Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

# Algorithm Exact Change inaccurate output!

See original GitHub issue

Firefox 47.0 Windows 10

``````
function checkCashRegister(price, cash, cid) {
var change = cash - price,
denom = [
["ONE HUNDRED", 100.00],
["TWENTY", 20.00],
["TEN", 10.00],
["FIVE", 5.00],
["ONE", 1.00],
["QUARTER", 0.25],
["DIME", 0.10],
["NICKEL", 0.05],
["PENNY", 0.01]
],
register = cid.reduce(function(acc, curr) {
acc.TOTAL += curr[1];
acc[curr[0]] = curr[1];
return acc;
}, {"TOTAL": 0});

// Here is your change, ma'am.

if (register.TOTAL < change) {
return "Insufficient Funds";
} else if (register.TOTAL === change) {
return "Closed";
}

var changeDue = [];
for (var i = 0; i < denom.length; i++) {
var currLeft = register[denom[i][0]],
val = 0;
while (change >= denom[i][1] && currLeft > 0) {
change -= denom[i][1];
currLeft -= denom[i][1];
val += denom[i][1];
change = Math.round(change * 100) / 100;
}

if (val > 0) {
changeDue.push([denom[i][0], val]);
}
}

if (change > 0) {
return "Insufficient Funds";
}

return changeDue;

}

checkCashRegister(1, 2.05, [["PENNY", 0], ["NICKEL", 0], ["DIME", 0.30], ["QUARTER", 0.75], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
``````

I passed all the FCC’s test cases. However, for the input above, output should be [[“DIME”, 0.30], [“QUARTER”, 0.75]] instead of “Insufficient Fund!”.

Similarly, for checkCashRegister(1, 6.05, [[“PENNY”, 0], [“NICKEL”, 0], [“DIME”, 0.30], [“QUARTER”, 0.75], [“ONE”, 1], [“FIVE”, 1], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]); output should be [[“DIME”, 0.30], [“QUARTER”, 0.75], [“FIVE”, 1]].

I looked through FCC wiki page for the solution code and still got the same issue. I’m still trying to solve this challenge taking into account the above cases though.

### Issue Analytics

• State:
• Created 7 years ago

1reaction
dskloetcommented, Aug 14, 2016

Instead you could make it explicit in the problem statement that the cashier will always try the maximum amount of each denomination and may fail to give change even though it is possible. “Insufficient funds” would have to be changed to “Failed to give change” or something.

1reaction
BKinahancommented, Jul 10, 2016

@blueirisss That is indeed more reasonable! It is certainly realistic and does require a more complex solution.

So the question up for discussion is whether a test case should be added to make the challenge more complex and difficult in order to make it a more accurate approximation of the problem represented.

At this point in the course, is it reasonable to expect users to learn how to generate the various possible combinations of change to see if it can be paid correctly, rather than simply attempting to pay as much as possible with the highest denominations downward?

#### Top Results From Across the Web

Exact Solution for “Exact Change” - freeCodeCamp
The method it uses is to use the highest denomination and go down until it reaches the goal or the lowest denomination. It...
Exact Change Algorithm - python - Stack Overflow
First you check if the recursion was successful (e.g. it found a valid result). If so, you return that result like normal. However,...
Facebook | Online | Change in a Foreign Currency - LeetCode
This is a practice problem from Facebook's recruiting portal: You likely know that different currencies have coins and bills of different denominations.
Why Do I Get Different Results Each Time in Machine Learning?
The impact is that each time the stochastic machine learning algorithm is run on the same data, it learns a slightly different model....
Coin Change | DP-7 - GeeksforGeeks
Note: Assume that you have an infinite supply of each type of coin. Examples: Input: sum = 4, coins[] = {1,2,3}, Output: 4....

#### Troubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free