Factorialize a Number with a (faster) loop

Tell us what’s happening:
How could this being scripted into a loop? (alternative which is supposed to be faster)

Your code so far


let factorialize = (num) => {
  return num == 0 ? 1 : num * factorialize(num-1)
}
factorialize(5);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119.

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

I used a while loop when
I did this
initialise variable, run it through the loop, return the variable

try figuring it out yourself

I tried that one, but I couldn’t land it. I’ll tell you…

post your code, we can work on it!

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

oh!
I have seen that blurred thing but I didn’t know how to use it

if you post what you tried with the loop we can see what you need to improve it!

First try:

function factorialize(num) {
let x = 1
  for (let i = 0; i < num; i++) {
    x *= x + 1
  }
  return x;
  
}

console.log(factorialize(0))// works just with 0

Second try:

function factorialize(num) {

  for (let i = num; i > 0; i--) {
    num *= num 
  }
  return num;
  
}

console.log(factorialize(5)// doesn't factorize anything

This --for me-- was the less far from any solution:

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

console.log(factorialize(5))// prompts 20 I think because It just iterates one time, but I couldn't understand why.

in none if your loops you use the changing variable - when you do recursion you get num * num-1 * ... * 2 * 1 because the argument of the function changes, but now you do not have any number that changes by one that you use. you have i but you are not using it