donacross / moravio-be-coins

Moravio - backend task - coins. https://hackmd.io/3MSmOwe3RV6Zoyzd4JUyOQ
MIT License
0 stars 1 forks source link

Change is calculated incorrectly for some coin systems #1

Open petrjoachim opened 3 years ago

petrjoachim commented 3 years ago

In some situations, getChange method doesn't return correct values. An example to this is input getChange(12, [1, 4, 5]) - it should result in three coins of value 4 which is the least amount of coins, but it returns two 5 coins and two 1 coins.

Actual behavior:

getChange(12, [1, 4, 5]);
=> { success: true, change: [5, 5, 1, 1], left: 0 }

Correct behavior:

getChange(12, [1, 4, 5]); 
=> { success: true, change: [4, 4, 4], left: 0 }
donacross commented 3 years ago

Thank you for your feedback. I got it wrong indeed and didn't covered the least amount of coin returned. I'll have a look tomorrow and get back to you.

donacross commented 3 years ago

Hi @petrjoachim,

I reworked getChange() and fixed the issue.

I let you have a look, the changes are published to npm.

25de8ab094b98bf456e8873596bc62a035b5491a