Factorialize a Number; Why is this code not working?

Factorialize a Number; Why is this code not working?
0.0 0

#1

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:


#2

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 ?


#3

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?


#4

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


#5

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:


#6

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