var elements = [5,4,3,2,1];
document.write("<br>Factorial: " + calculateFactorial(elements));
//factorial function Ex. 5! = 5*4*3*2*1 = 120
function calculateFactorial(values){
var result = [];
for (i = 0; i < values.length; ++i){
var factorialValue = 1;
for (j = 1; j <= values[i]; j++){
factorialValue = factorialValue * j;
}
result.push(factorialValue);
}
return result.join(",");
}
I have developed a program that takes an array of random numbers as input from the user and then calculates different metrics based on those numbers.
The current focus is to compute the factorial of each individual number in the array and present them in sequence.
However, the current implementation is resulting in NaN being displayed instead of the desired output.
What errors do you think might be causing this issue? Is there something crucial missing from my code?
CURRENT OUTPUT
Factorial: NaN
EXPECTED OUTPUT
Factorial: 120,24,6,2,1