Create a function to calculate factorial

Hello there,

Codecademy give the solution below for creating a function that computes a factorial

// Write function below

const factorial = n => {

let result = 1;

for (let i=n; i>0; iā€“) {

result *= i+1;

}

return result;

}

But factorial(3) produces 26
instead of 6

Can you explain??

Unfortunately, the solution in that interactive article is incorrect. The test for that portion of the article is correct and will pass with a proper solution, but the code provided when using the View Solution option is wrong.

result *= i+1; should be result *= i;

4 Likes