Factorialize a Number - Help

Factorialize a Number - Help
0

#1
var p = 1;
function factorialize(num) {
  for (var i = num; i > 0; i--) {
    p *= i;
  }
  return p;
}

If I put the test numbers in, I get the correct answer in the console. Why is it not passing the tests?


#2

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2