Output is correct but not giving credit (120)

Tell us what’s happening:

Your code so far


function factorialize(num) {
  for (i =1;i <=num;i++){
    newNum *= i;
 
  }
 num = newNum;
  return num;
}

factorialize(5);

Your browser information:

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

Link to the challenge:

https://www.freecodecamp.org/challenges/factorialize-a-number

You never defined newNum.

In the future, please fill out the “Tell us what’s happening” section.

it is defined in my code. don’t know why it didn’t copy that part:

newNum = 1;

function factorialize(num) {
for (i =1;i <=num;i++){
newNum *= i;

}
num = newNum;
return num;
}

factorialize(5);

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
1 Like

Ahh thank you so much!