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.

Your code so far


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

factorialize(5);

Your browser information:

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.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number/

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

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);

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);

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?