Factorialize a Number - Write answer not passing

Factorialize a Number - Write answer not passing
0.0 0

#1

Tell us what’s happening:

Hi all,

This code I’ve written seems to have passed all tests but does not pass the check mark. Do I need to do it a different way? Thank you.

Your code so far

function factorialize(num) {
  for (var i = 0; i < num; i ++)
   
    x=x*(num-i);
  return x;
}

factorialize(0);

Your browser information:

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

Link to the challenge:


#2

What is x? Where did you define it?


#3

I did define it above it just didn’t paste in automatically.

var x = 1;
function factorialize(num) {
for (var i = 0; i < num; i ++)

x=x*(num-i);

return x;
}

factorialize(0);


#4

You are using global variables.

Global variables persist after function calls have completed, so we have to be very careful about modifying globals in functions. Your code relies on x being 1 when the function is called, but after factorialize has been executed it is no longer 1. This means that your function will only work once.


#5

Thanks for that response. I knew when I was writing it it wasn’t the best solve. My first few attempts at the solve were very close to what I now see was my first try. I looked at the spoiler and just see that I was off on the condition statement in my for loop. Thanks again.