Factorialize a Number; Why is this code not working?

Tell us what’s happening:

Your code so far

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

factorialize(5);

Your browser information:

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

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

Hey,

when you call return, the function stops,

So basically, here you are doing :

i=1
return 1*5
end;

Are you trying to do an iterative or a recursive function ?

I am trying to solve the challenge with a for cycle and for this reason I tried with the code I posted above. In my opinion it is supposed to be iterating until the variable “i” which starts from 1 reaches the number “num” but in fact it ends as you said at 5. So, can you explain to me where my logic goes wrong and how can I rewrite the code so that it does just that?

Here’s a link to the challenge: https://www.freecodecamp.org/challenges/factorialize-a-number

As I told you, the function stops as soon as it reached a return,
You need your for loop to end, so the return call must be after the loop (generally speaking, never put a return call in a loop).

Try to put the return call after the loop.
If you still have struggle with it, I stay around with more hints :slight_smile:

can anyone help me get past this challenge on Basic Algorithm Scripting-Factorialize (5)

i can help you if you are still stuck

i can help you .you need to initialise the bottom layer of recursion which is given their .The principal of recursion distributes the work among several layers but the actual work is done at downmost layer .That means at the “cell - level”. so mention what should your code do when you have num= num * factorialize(num - 1)
because when you are finding factorial of 5 code will work like
5 = 5 * factorialize(5 - 1) and its calling “factorialize” function again. now think what you code should produce when factorialize (1 - 1) will occur .So you need to define what should be the function value when num = 0. By defining that you have defined the last layer of recursion and now your code will run and it will give you result as 54321=?.
remember that this problem is of “Recursion” . In recursion function call itself again in function definition.