Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
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
 
Old July 27th, 2003, 10:14 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to mattrd
Default 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???
 
Old July 28th, 2003, 05:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.