Solution Review: Create an Amount
This lesson will discuss the two possible solutions to the challenge in the previous lesson: recursive and dynamic.
Solution: recursive #
Let’s start by discussing the recursive solution to this problem.
Press + to interact
function returnWays(coins, numOfCoins,amount) {if(amount === 0) return 1; // only one way to return zero amountif(amount < 0) return 0; // No solution exists for negative amountif(numOfCoins < 0 && amount > 0) return 0; // If no coins leftreturn returnWays(coins, numOfCoins, amount - coins[numOfCoins]) +returnWays(coins, numOfCoins - 1, amount);}var coins = [1, 2, 3];var amount = 4;console.log(returnWays(coins,coins.length-1,amount));
Explanation
...Access this course and 1400+ top-rated courses and projects.