Algorithm Exact Change inaccurate output!
See original GitHub issueExact Change https://www.freecodecamp.com/challenges/exact-change
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
- Comments:17 (4 by maintainers)
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...
Read more >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,...
Read more >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.
Read more >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....
Read more >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....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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?