Factorialize a Number using For loops

Tell us what’s happening:
Can anyone tell me what is wrong with this solution because I am not able to complete the challenge but every time I console.log it gives me the correct answer

Your code so far


var n = 1
function factorialize(num) {
if (num == 0) {
  return 1
} else if (num !== 0){
  n = n * num
  num--
  factorialize(num) 
}
return n;
}

console.log(factorialize(20));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36.

Challenge: Factorialize a Number

Link to the challenge:

Try testing your code with two function calls:

console.log(factorialize(20));
console.log(factorialize(5));

the second result will surprise you


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 previous 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