RosettaCodeData/Task/Count-the-coins/JavaScript/count-the-coins-3.js

26 lines
492 B
JavaScript
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
function countcoins(t, o) {
2017-09-23 10:01:46 +02:00
'use strict';
var operandsLength = o.length;
var solutions = 0;
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
function permutate(a, x) {
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
// base case
if (a === t) {
solutions++;
}
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
// recursive case
else if (a < t) {
for (var i = 0; i < operandsLength; i++) {
if (i >= x) {
permutate(o[i] + a, i);
}
}
}
}
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
permutate(0, 0);
return solutions;
2013-04-10 16:57:12 -07:00
}