Results 1 to 2 of 2

Thread: newb's code homework, help!

  1. #1
    lee551's Avatar no soup for you! BT Rep: +5
    Join Date
    Jan 2004
    Posts
    1,740
    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:
    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

  2. Internet, Programming and Graphics   -   #2
    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
    Last edited by ilw; 10-29-2005 at 12:42 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •