Help with Factorialize a Number

Tell us what’s happening:
I can’t seem to figure out why this isn’t working. I wrote it out on paper and it seems like it should work correctly?
What I wrote out is this:
num=num*(num-1)
num=5*(5-1)=20
num=20*(4-1)=60
num=60*(3-1)=120
num=120*(2-1)=120

Your code so far


function factorialize(num) {
  let a;
  for(a=1; num>1; num--){
   num*=(num-a);

    console.log(num);
  
  }
  return num;
}

factorialize(5);

Your browser information:

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

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

Careful, what is changing during your loop?
You are decrementing num by one, but st the same time you are greatly increasing num With the multiplication
num > 1 will never be false, you have an infinite loop

Hey I got everything except factorialize(0) should return 1 to work. Not sure how to do that since anything multiplied by 0 is just 0?

function factorialize(num) {
  let a;
  let factNum=num;
  for(a=1; num>1; num--){
  factNum*=(num-a);
console.log(factNum);
  }
  return factNum;
}
factorialize(5);

Edit: Nvm I got it with this: Is it a valid way of doing it?

function factorialize(num) {
  let a;
  let factNum=num;
  for(a=1; num>1; num--){
  factNum*=(num-a);
  }
console.log(factNum);
if(num==0){
  return 1;
}
  return factNum;
  }
factorialize(5);

You are getting the right result, and that is important
But you could do it with a few small changes using just one return statement

Is this what you meant? Also thank you for all the help!

function factorialize(num) {
  let a;
  let factNum=num;
  for(a=1; num>1; num--){
  factNum*=(num-a);
  }
console.log(factNum);
if(num==0){
  factNum=1;
}
  return factNum;
  }
factorialize(5);

Not really, but let’s see a few other things

You are also using this loop in a weird way, the way you are using variables you would better use a while loop
If you want to use a for loop you would do better creating an iterating variable for that loop
for (let i = initialValue; conditionToCheck; change_i)

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

Is this better? I was initially trying to do it this way but I got confused trying to solve it while changing the i variable but I think doing it the way I initially posted and then going back and trying it this way helped a lot :D.

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

Edit: Also did it with a while loop

function factorialize(num) {
  let factNum=num;
  while(num>1){
    factNum=factNum*(num-1);
    num--;
  }
  if(num===0){
    return 1;
  }
  return factNum;
}

factorialize(5);