```
var coinChange = function(coins, amount) {
var dp=new Array(amount+1);
dp.fill(Number.MAX_VALUE-1);
dp[0]=0;
for(var i=1;i<=amount;i++)
{
for(var j=0;j<coins.length;j++)if(coins[j]<=i){
dp[i]=Math.min(dp[i-coins[j]]+1, dp[i]);
}
}
return dp[amount]==Number.MAX_VALUE-1 ? -1:dp[amount];
```

};