JSFiddle - React, Tailwind, and code Playground

by mschock

JavaScript

var count = 0;

function sort(denoms) {
    return denoms;
}

function combos(amount, denoms) {
    denoms = sort(denoms);
    getCounts(amount, denoms);
}

function getCounts(amount, denoms) {
    for (var i = 0; i < denoms.length; i++) {
        var denom = denoms[i]; //1
        var maxNumTimesToUseDenom = Math.floor(amount / denom); //4/1
        for (var j = 1; j < maxNumTimesToUseDenom + 1; j++) {
            var newAmount = amount - j * denom;
            if (newAmount === 0) {
                count++;
            } else if (newAmount < 0) {
                continue;
            } else {
                var newdenoms = denoms.slice(i + 1);
            	getCounts(newAmount, newdenoms);
            }
        }
    }
}

combos(4, [1, 4, 3, 2]);

console.log('count: ', count);