Factorialize a Number! wondering if it works

Tell us what’s happening:
my code is working on the console.log, but with the tutorial it is not. is it a problem with my algorithm? i wanted to use recursion becuase i am taking algorithm class in school right now, do i have to use a for loop ?

Your code so far


var i=1;
function factorialize(num) {
if (num>1){
factorialize(num-1)
}
i = i*num
return i;
}

factorialize(5);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number

You’re using a global variable, that won’t work because it will update each test (first one will be correct, then i will start as the result of that and so on). It’s fine to use recursion, but that would not be how you do it.

1 Like

got it, thank you! recursion is still confusing for me and i shouldn’t use it in these tutorials.
weird enough, when i copy and paste the code on my editor and run it, it actually works with any number i try.