[Solved] How to get Factorial of number in JavaScript? [duplicate]


The pictured code (please include actual code in the future, not screenshots of code) returns fact immediately:

for ( n = 1; n <= num; n++ ) {
  return fact * n;
}

since n starts at 1.

What you want is to include fact in the function, and multiply it as the loop goes along, then return:

function factorial(n) {
  var fact = 1;

  for ( n = 2; n <= num; n++ ) {
    fact = fact * n;
  }

  return fact;
}

2

solved How to get Factorial of number in JavaScript? [duplicate]