Task of Challenge #06/54

Calculate 9! a for-loop .

Solution

Explanation

To create the factorial of 9 we make almost the same as in challenge #05 but with a different loop body code block.

Last time we used this statement to gather the accumulated sum sum+=i. Knowing a bit about mathematics means you are aware of the fact that a factorial is nothing more than a chained multiplication.

So 9! is nothing more than 9*8*7*6*5*4*3*2*1. Together with the knowledge about for-loops we can draw advantages out of this by using the running index of i to create a factorial. To get our desired result, we iterate exact 9 times!

Result of 9! printed out to the console

Follow me on Instagram and don’t miss the latest Challenge!

https://www.instagram.com/arnold.code/

Gather more Knowledge about JavaScript?

Here are two pre-picked and high quality Medium articles for you.