Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. function needsChange(amount) {
  2. const coinsSorted = [a, b, c].sort((x, y) => x < y);
  3.  
  4. function iter(sum, currentCoinIndex, coinsLeft) {
  5. console.log(arguments);
  6. const newSum = sum - coinsSorted[currentCoinIndex];
  7. const nextCoinIndex = currentCoinIndex + 1;
  8.  
  9. if (newSum === 0) {
  10. return false;
  11. }
  12.  
  13. if (newSum < 0) {
  14. return coinsLeft ? iter(sum, nextCoinIndex, nextCoinIndex < coinsSorted.length) : true;
  15. }
  16.  
  17. if (newSum > 0) {
  18. return iter(newSum, currentCoinIndex, nextCoinIndex < coinsSorted.length); }
  19. }
  20.  
  21. return iter(amount, 0, true);
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement