Basic Algorithm Scripting: Factorialize a NumberPassed

I want to store the result of the first cycle and I want to multiply for the next one.

function factorialize(num) {
 
 for (let i=1;i<=num;i++){
           
           let b =i*(i-1)

           //let a=i; 
          
          
          console.log(b)
      
                         
 }

  return num;
}

factorialize(3);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

2 Likes

as the variable initialization is inside the loop, it will be created anew at each iteration. Also, b is not accessible outside of loop.
You need a variable created at function level, and then updated inside the loop.

1 Like

I want to multiply the result of every cycle.

function factorialize(num) {
 
   for (let i=1;i<=num;i++){
        
        let valCyc=i*(i-1); 
     
     //console.log(valCyc);


   }
           
}

factorialize(3);


when you initialise a variable inside a loop, then it will exist only in the single iteration. It will be created anew each time. If you want instead to change the variable, you need to declare it outside the loop…

see for example this challenge

1 Like

I thankfun for sharing your knowledge with me. Thanks for your time.