Factorialize a Number - Why does this work with node js but not here?

Factorialize a Number - Why does this work with node js but not here?
0

#1

I’m working on the Factorialize a Number challenge. I decided to start working with node js so I could see more run results than just that last line that FCC offers. The code I came up with produces the right numbers in node js, but FCC tells me I’m not returning a number:

var factor=1;

function factorialize(num) {

   if (num==0) {
     console.log(factor);
     return factor;} else {
  factor=factor*num;
  factorialize(num-1);}

}  

factorialize(5);

What am I missing? I figure I must be missing something because I needed the console.log(factor) to get a printout from node js. If I leave it at return factor; I don’t get numbers in node js either. But then code that will correctly return a value in FCC like:

return("hello world");

don’t work for me in node.js either. I’ve looked up documentation on return and on node js and still can’t figure out what this code is unacceptable to FCC. Thanks for any light you can shine my way.


#2

Because you declared factor as a global variable, when the FCC tests run one after another, factor retains the value it had in the last time factorialize ran in a previous test.


#3

Thanks. I did try declaring factor within the function and that did not seem to work out either. More work ahead of me. But the global issue does make sense.

There’s also the issue that in FCC, my return statement prints out nothing at all, rather than an incorrect value.