# Solution for Rosetta Code: Count the coins

What is your hint or solution suggestion?

This isn’t the fastest solution, but it’s one of the simplest solutions. The key equation for coin counting is equal to `25*q + 10*d + 5*n + 5*p = 100`. In this case, we can see that pennies can only be counted in batches of five for this problem, hence why there is a coefficient of 5 for p.

The limits for `(q,d,n,p)` are `(0-3,0-9,0-19,and 0-19)`. All possible subarrays of `[q,d,n,p]` are created, then `Array.map` is used to plug them into the equation `25*q + 10*d + 5*n + 5*p`. Then, the values are filtered so that only instances where the equation is equal to 100 remain. There are 238 instances, as I left off the 4 cases where all coins are the same, and 238+4 = 242.

Note: If you want to try something faster, learn about what is “Dynamic Programming”.

Solution 1
``````function countCoins() {
var quarter = Array.from(Array(4).keys());
var dime = Array.from(Array(10).keys());
var nickel = Array.from(Array(20).keys());
var penny = Array.from(Array(20).keys());

var parts = [quarter,dime,nickel,penny],
result = parts.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));

var result1 = result.map((sub) => sub*25 + sub*10 + sub*5 + sub*5);

const result2 = new Map([...new Set(result1)].map(
x => [x, result1.filter(y => y === x).length]
));

return result2.get(100)+4;
}
``````

Challenge: Count the coins

Link to the challenge: