The browser storage localStorage is not available. Either your browser does not support it or you have disabled it or the maximum memory size is exceeded. Without localStorage your solutions will not be stored.
Factorial
Let us practice the
for
loop with the
factorial used in mathematics.
The factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.
For example, 3! = 1*2*3 = 6. By definition, 0! = 1.Exercise
Write a function
Example:
factorial
that calculates the factorial of a positive integer.Example:
factorial(3)
should return 6
.
+ Hint
function factorial(n) {
let result = 1;
for (...) {
...
}
return result;
}
+ Solution
function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result = result * i;
}
return result;
}