Need Help With Javascript Question - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Need Help With Javascript Question (1191 Views)
1 Reply
| Need Help With Javascript Question by kaykay100(op): 1:38am On Feb 18, 2019 |
I want a solution to this question in javascript as I am new to coding. Below is my attemp but it is not working. Question; Write a function that counts how many different ways you can make change for an amount of money, given an array of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomination 1 and 2: 1+1+1+1, 1+1+2, 2+2. The order of coins does not matter: 1+1+2 == 2+1+1 .Also, assume that you have an infinite amount of coins.Your function should take an amount to change and an array of unique denominations for the coins: countChange(4, [1,2]) // => 3 my code attempt,but not working; function countChange(coins, money) { var m = money; var n = coins.length; var dp = Array(m + 1).fill().map(() => Array(n).fill(0)); // Fill the entries for money = 0 for (var i = 0; i < n; i++) dp[0][i] = 1 for (var i = 1; i <= m; i++) { for (var j = 0; j < n; j++) { // Count of solutions inculding coins[j] var x = i - coins[j] >= 0 ? dp[i - coins[j]][j] : 0 // Count of solutions excluding coins[j] var y = j > 0 ? dp[i][j - 1] : 0 dp[i][j] = x + y } } return dp[m][n - 1] } console.log(countChange); |
| Re: Need Help With Javascript Question by tollyboy5(m): 2:40am On Feb 18, 2019 |
Lemme sleep and come back in the morning first |
| Re: Need Help With Javascript Question by Fasholu: 8:31pm On Mar 11, 2019 |
The solution is right, but you need to pass necessary parameters to the "countChange" function. |
July Bootcamp Training - Full Stack Web Development With Javascript (mern Stack) • Tarot Website I Cooked With JavaScript,HTML • Mobile Technology: Lets Build A Mobile App With Javascript • 2 • 3 • 4
Is Udemy Video Streaming Much Slower Than Youtube • Free Web Development And Programming Training On Whatsapp • Where To Learn Programming/software Development In Ilorin Kwara State