www.richandlucy.com © www.richandlucy.com
Home | Photos | Stuff | Forum

> Stuff > Lucy's Portfolio! > Javascript Example 9

For any given number n, calculate n! or n factorial.

n! = n*(n-1)*(n-2)*…*2*1=n*(n-1)!
0! = 1
1! = 1
2! = 2*1 = 2
3! = 3*2*1 = 3*2!=6
4! = 4*3*2*1 = 4*3!=24
5! = 5*4*3*2*1 = 5*4!=120

Etc...

Create a table to input the value of n, to display the answer and for a button to start the calculation.

A function will be required which calls itself (this is called recursion) e.g. if your function is called factorial(n) there will be a line in it that calls n*factorial(n-1).


You can see the page created here


2 3 4 5 6 7 8 9 10 11 12 13
© www.richandlucy.com - 2004 All content created and owned by www.richandlucy.com | Contact us