Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,975 members, 7,806,836 topics. Date: Wednesday, 24 April 2024 at 03:38 AM

Need Help With Javascript Question - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Need Help With Javascript Question (1102 Views)

Tarot Website I Cooked With JavaScript,HTML / Mobile Technology: Lets Build A Mobile App With Javascript / My Semester GPA Calculator With Javascript (2) (3) (4)

(1) (Reply)

Need Help With Javascript Question by kaykay100(f): 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)

I Need An App For My Website Integrated With Admob / Download Free Guide To Become Big Data Engineer 2020 / Pack Of Sock5 Proxy Available For Sale!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.