PDA

View Full Version : newb's code homework, help!



lee551
10-29-2005, 03:49 AM
i have to do a few simple calculations for my javascript programming class and i've reached a part that i'm stuck at.

i have an array, made up of a user-determined number of user-determined numbers. what they want me to do is write code to calculate the factorial of a user determined position (ie. the factorial of position 3 [ie. position3*position2*position1]).

what i have now is the extremely iffy code:

var i = window.prompt("Factorial from what position?");
for (x=0;x<=i;x=x+1){
product=myarrayname[x]*myarrayname[x+1]
}
document.write("product of a factorial done from position " + (i) + ":<br />")
document.write(product);

i know that the way they are multiplied right now doesn't make sense, but i have been trying to work it all different kinds of ways.

i have to turn it in in an hour or so, quick responses = sexual favor!!! haha

ilw
10-29-2005, 12:38 PM
don't do java, but its all kinda the same

initialise 'product' to be equal to 1 outside the loop then the line inside the loop should be

product = product * myarrayname[x]




remember to reset product to 1 if you reuse the loop

edit: Remember factorials can get very big very quick, so make the product variable capable of handling very large numbers and you should probably try to include some checks to make sure it doesn't exceed the maximum value for that variable type