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

20 lines
454 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 targetsLength = t + 1;
var operandsLength = o.length;
t = [1];
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
for (var a = 0; a < operandsLength; a++) {
for (var b = 1; b < targetsLength; b++) {
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
// initialise undefined target
t[b] = t[b] ? t[b] : 0;
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
// accumulate target + operand ways
t[b] += (b < o[a]) ? 0 : t[b - o[a]];
}
}
2013-04-10 16:57:12 -07:00
2017-09-23 10:01:46 +02:00
return t[targetsLength - 1];
2013-04-10 16:57:12 -07:00
}