Factorialize a Number - recursive function

Factorialize a Number - recursive function
0

#1

I made a recursive function. It gives me the right answers, but freecodecamp not check it

var answ=1;
function factorialize(num) {
if (num!=1){
answ *= num;
num–;
factorialize(num);
}
return answ;
}

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/62.0.3202.94 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/factorialize-a-number

#2

The global variable answ outside the function is causing the trouble for you. The FCC tests call your function consecutively, so since answ is global, it retains the value at the end of the last function call.

Put a console.log(answ) on the first line of your function and you will see answ does not start at 1 after the 1st test.


#3

so, how can i make local answ without breaking the code?


#4

You need to rethink your logic just a bit.

You could use two return statements and answer the following questions:

  1. What do you return if num is equal to 0 or 1?
  2. What do you return if num is greater than 1?

The answer to the 2nd question requires you to return num multiplied the facorialization of the next num, which would be num -1. Keep trying and you will figure it out. You may need to put some console.log statements in so you can see the values of various variables before they are returned.