₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,671 members, 8,513,085 topics. Date: Wednesday, 04 November 2026 at 12:33 PM

Toggle theme

Need Help With Javascript Question - Programming - Nairaland

Nairaland Forum › Science/Technology › Programming › Need Help With Javascript Question (1209 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.
1 Reply

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

How To Find And Hire A Python/django Development Company • Experienced Programmers Please Come In • Admission Into Decagon Institute