Skip to content

Instantly share code, notes, and snippets.

@Arieg419
Created December 27, 2016 15:58
Show Gist options
  • Save Arieg419/cdca8d2b8c17e68de2a6bd2e1a8b664f to your computer and use it in GitHub Desktop.
Save Arieg419/cdca8d2b8c17e68de2a6bd2e1a8b664f to your computer and use it in GitHub Desktop.
function waysToReturnChange(denominations, numOfCoins, amount) {
if(amount === 0) return 1; // Perfect!
if(amount < 0) return 0; // No solution exists for negative amount
if(numOfCoins < 0 && amount > 0) return 0; // We don't have coins left!
console.log('checking ways to make ' + amount + ' with ' + denominations.slice(numOfCoins));
return waysToReturnChange(denominations, numOfCoins, amount - denominations[numOfCoins]) +
waysToReturnChange(denominations, numOfCoins - 1, amount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment