Wrox Programmer Forums doing a factorial using a for loop
 |
 Javascript How-To Ask your "How do I do this with Javascript?" questions here.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

July 27th, 2003, 10:14 PM
 mattrd Registered User Join Date: Jul 2003 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts
doing a factorial using a for loop

can anyone tell me how to do a factorial using a for loop?
I understand how to make a list adding a number to each line such as

for (i = 0; i <= 5; i++)
{
document.write("The number is " + i)
document.write("<br>")
}

but can anyone tell me how to do a factorial using a for loop???

July 28th, 2003, 05:11 AM
 pgtips Friend of Wrox Join Date: Jun 2003 Posts: 1,212 Thanks: 0 Thanked 1 Time in 1 Post

Code:
```var factorial = 1;
for (i = 1; i <= 5; i++)
{
factorial *= i;
}```
will do factorial 5, I'm sure you can figure out from there how to generalise it...

 Similar Threads Thread Thread Starter Forum Replies Last Post Factorial of numbers 1 to 5 sivakak Javascript 1 May 24th, 2007 09:31 AM Factorial of numbers 1 to 5 sivakak BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 May 24th, 2007 08:42 AM loop crmpicco Classic ASP Basics 1 February 12th, 2005 09:12 PM 1000000 Factorial new_bie BOOK: Beginning Java 2 1 October 29th, 2004 12:01 PM nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM