# Factorialize a number, what did I do wrong?

Hi! Ive just started doing a challenge and this is my second one! I am aware it will take time to get the correct answer and I am happy to learn and do it again, but I would like to know why did the original code I wrote not work. It meets the requirements to pass the challenge but I simply can’t get a green tick. Here is my code:
"var array = [ ]];
function factorialize(num) {
var i=num;
if (i>0){
while (i>0){
array.push(i);
i–;
}

return previous*current;
});
}
else
return 1;
}

factorialize(5);"

Eugene

``````var array = [];
``````

``````function factorialize(num) {
var array = [];
``````

The reason this fixes it is because your array is never cleared. Every time you call factorialize you are just pushing more numbers into the array; thus, the tests start to fail.

I’d also say figure out additional ways to solve this problem, because your currently solution is a lot more complicated than it needs to be.

thank you! I was aware that mine may be complicated but its good to have someone confirming that too!

Eugene

Cool, I’ll be waiting here for you to post your alternative, less complicated, solution

@KhorEugene, first of all, sorry for my english.

Second: have you ever heard about Recursion? This is best way to solve your problem.

Think about it: you need a function that receive a number x and multiply it by x - 1. So you need a function that receives a number and, inside its, return x * a call of function again, passing x - 1 as parameter.

And don’t forget to check in begging of function if parameter is equal to 1, then return 1.

Hmm… Is that clear?

I didn’t know about Recursion! Yes that sounds like what I had in mind but I couldn’t think of much with what Ive learnt so far, thanks for telling me and Ill check it out!
One thing I realised is that these challenges are not limited to just what I’ve learnt previously if I want to succeed!

Yes

But remember, you can done these challenges with what you’ve learnt, but you are learning that we have more than one way to solve a problem, and some ways are better than others.