 # Factorialize a Number HELP AND GUIDANCE

Tell us what’s happening:
I’m stuck on this one.
My idea with the code below is that I want to establish the basic concept of factorials with ` n = 0` and `return num =1` and hope that descends into the code below it. However, that’s as far as I’ve been able to get. I have a low confidence in the previous theory I espoused.

So, the main thing I’m looking for is nudges toward what array modes I should probably be using. I’ve seen reduce() but I’m not sure how this would help. .push() seems right but I’m not sure how to contextually use it.

Also, any tips on thinking through algorithms. Or, websites that help to orient one’s thinking in this manner would be helpful as well.

Thank you.

``````
function factorialize(num) {
if(n = 0) {
return num = 1;
} else {
return num *= n;
}
}

factorialize(5);
``````

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

Where did you define n? Is it a global variable?

How would you solve a factorial problem with pen and paper? Write out the steps (logic) in plain language (no code) you use to calculate it manually on paper and then test your written logic with simple test cases like 5! to see if it would work. Once you have the correct written steps, then you convert that logic to code.

I was sketching this out on paper and I wasn’t able to come to a satisfactory solution.

My idea was that given a number (5, for example) one would multiply it by all of its preceding numerals. Therefore, I thought the simplest code would be num = n!, but this doesn’t pass.

My other idea is to somehow utilize nn-1n-2…

Perhaps in the above case I would use the spread operator. But I’m not sure.

Am I onto something in any way with the above suggestions?

``````
function factorialize(num) {
num = n!;
return num;
}

factorialize(5);
``````

In math, 5! is 5 factorial, but JavaScript does treat the ! after a number as factorial, so that is why it is not possible.

So, you did you try to write out the steps it would take you on paper to manually calculate 5 factorial? What would be your first step, 2nd step, etc, final step which would give you the solution of 120? You should write in steps as if you were having to give instructions to somebody that has never calculated 5 factorial before. Once you write out the steps, you will see a pattern which you can then use in developing your coded solution.

Below I have the steps I’ve written out and my attempt to transcribe them into code.

1.List numbers preceding 5
2.Multiply first two numbers of list
3. Multiply product of first two numbers by next number in list
4. Continue until end of the list is reached

This code at the bottom is very wrong, but it’s my first attempt. Here’s what I was attempting. I want the factorial to be equal an initial product of the first two numbers and number 3. and 4.

I’m using regex and starting with \$. The second \$ is an attempted to allow other numbers to be inserted into this equation.

I’m not sure how to order to the computer to continue multiplying until reaching the final number. The only thing that I could think of was using ^ to signal the end.

As you can see, I have a lot of work to do. However, if I’m in any way faintly touching on something right, then please nudge me in that direction.

CODE:

``````
function factorialize(num) {
n! = (\$(n*n-1)*\${num}^);
return num;
}``````

Okay. I think I figured this out but it’s still not working.

I’m not sure what I need to adjust.

``````
function factorialize(num) {
if(num === 0) {
return 1;
}
for(i = num-1;i>=1;i--) {
return num;
}
}
factorialize(5);
``````

Inside your for loop, you initialize i to num-1, so i starts at 4 in the example test call of factorialize(5), then the for loop condition of i >= 1 is checked (which is true), so you enter the first iteration of the for loop. Immediately, you return num and the function exits and returns 5.

Remember, when a return statement executes, the function immediately exits and does not return to finish up a loop. You need an extra variable to hold the current value of the factorization of num. It needs to be initialized to some value (I will let you figure that out) and then inside a for loop, you need to multiply this variable by each value of i and reassign the product back to the variable. This way, when the loop ends, this variable will contain the product of all the numbers which make up the factorial of 5.

Okay. I’d want to check that my logic is secure (that I understand what is happening) for the problem below.

``````
function factorialize(num) {

if(num === 0) {
return 1;
}

for(var i = num-1;i>=1;i--) {
num = num*i;
}
return num;
}

factorialize(5);
``````

If a given number assigned the variable num is 0, then it will be factorialized as 1.
For number assigned the variable i that are greater than or equal to one and subtracted by one and iterated decrementally by one so that given number for the variable i is multiplied by the initial value of num.
Return the value of num for number meeting the requirements of this loop.

Am I correct in how I am explaining this?