I've been working on a function to find the factorial of an integer and then reducing the array containing factorials (multiplying each element).
For instance:
factor(5) >>> [1, 2, 3, 4, 5] >>> 1 * 2 * 3 * 4 * 5 >>> 120
var factorialArray = [ ];
function factor(num) {
for (i = 1; i <= num; i++) {
factorialArray.push(i);
}
return factorialArray.reduce(function(a, b) {
return a * b;
}, 1);
};
console.log(factor(5));
Unfortunately, it seems to be giving me an undefined result.
I suspect that there might be something wrong with the function's structure, but I'm unsure.