We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hey I have a javascript program done the same way as OP, however I'm getting a runtime time error with my "done" array. I'm new to JS so can somebody help me spot the issue?
functiongetWays(n,c,m,done){// Complete this functioncvarres=0;if(n==0){return1;}if(n<0)return0;if(m<=0)return0;else{// Here is where I get the error: TypeError: Cannot read property '2' of undefinedif(done[n][m]==-1)done[n][m]=getWays(n-c[m-1],c,m,done)+getWays(n,c,m-1,done);returndone[n][m];}}functionmain(){varn_temp=readLine().split(' ');varn=parseInt(n_temp[0]);varm=parseInt(n_temp[1]);vardone=[];c=readLine().split(' ');c=c.map(Number);for(vari=0;i<n;i++){varcol=[];for(varj=0;j<m;j++){col[j]=-1;}done[i]=col;}// Print the number of ways of making change for 'n' units using coins having the values given by 'c'varways=getWays(n,c,m-1,done);console.log(ways);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Coin Change Problem
You are viewing a single comment's thread. Return to all comments →
Hey I have a javascript program done the same way as OP, however I'm getting a runtime time error with my "done" array. I'm new to JS so can somebody help me spot the issue?