Web Analytics Made Easy -
StatCounter can anyone help me with this? - CodingForum

Announcement

Collapse
No announcement yet.

can anyone help me with this?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • can anyone help me with this?

    hey all, im new here(first post),being relativly new to javascript i don't know that much. can anyone give me any help on this. its part of a 'dice roller' , is there any way to do this without useing lots of if statements? and what would be causing it to get 'stuck' on "if(d=='4')" and only do that part of the script? thanks in advance.
    /////////
    var num=0;
    var ufl=0;
    var d=0;
    function nugen()
    {
    rond=new Array()
    rond[0]=Math.floor(Math.random()*(eval(num))+1);
    rond[1]=Math.floor(Math.random()*(eval(num))+1);
    rond[2]=Math.floor(Math.random()*(eval(num))+1);
    rond[3]=Math.floor(Math.random()*(eval(num))+1);
    rond[4]=Math.floor(Math.random()*(eval(num))+1);
    rond[5]=Math.floor(Math.random()*(eval(num))+1);
    rond[6]=Math.floor(Math.random()*(eval(num))+1);
    rond[7]=Math.floor(Math.random()*(eval(num))+1);
    rond[8]=Math.floor(Math.random()*(eval(num))+1);
    rond[9]=Math.floor(Math.random()*(eval(num))+1);
    rand();
    }
    function rand()
    {
    zb();
    ufl=document.form1.numofdice;
    num=document.chxbxs.optionchoose.value;
    window.status="you are now rolling a D"+num;
    if(d=="1")
    {
    document.form0.resultbx0.value=rond[0];
    }
    if(d=="2")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    }
    if(d=="3")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    }
    if(d=="4")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    }
    if(d=="5")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    document.form0.resultbx4.value=rond[4];
    }
    if(d=="6")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    document.form0.resultbx4.value=rond[4];
    document.form0.resultbx5.value=rond[5];
    }
    if(d=="7")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    document.form0.resultbx4.value=rond[4];
    document.form0.resultbx5.value=rond[5];
    document.form0.resultbx6.value=rond[6];
    }
    if(d=="8")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    document.form0.resultbx4.value=rond[4];
    document.form0.resultbx5.value=rond[5];
    document.form0.resultbx6.value=rond[6];
    document.form0.resultbx7.value=rond[7];
    }
    if(d=="9")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    document.form0.resultbx4.value=rond[4];
    document.form0.resultbx5.value=rond[5];
    document.form0.resultbx6.value=rond[6];
    document.form0.resultbx7.value=rond[7];
    document.form0.resultbx8.value=rond[8];
    }
    if(d=="10")
    {
    document.form0.resultbx0.value=rond[0];
    document.form0.resultbx1.value=rond[1];
    document.form0.resultbx2.value=rond[2];
    document.form0.resultbx3.value=rond[3];
    document.form0.resultbx4.value=rond[4];
    document.form0.resultbx5.value=rond[5];
    document.form0.resultbx6.value=rond[6];
    document.form0.resultbx7.value=rond[7];
    document.form0.resultbx8.value=rond[8];
    document.form0.resultbx9.value=rond[9];
    }
    dctt();
    }
    function dctt()
    {
    document.form0.dicetotals.value=eval(bz0)+eval(bz1)+eval(bz2)+eval(bz3)+eval(bz4)+eval(bz5)+eval(bz6 )+eval(bz7)+eval(bz8)+eval(bz9);
    }
    function zb()
    {
    bz0=document.form0.resultbx0.value
    bz1=document.form0.resultbx1.value
    bz2=document.form0.resultbx2.value
    bz3=document.form0.resultbx3.value
    bz4=document.form0.resultbx4.value
    bz5=document.form0.resultbx5.value
    bz6=document.form0.resultbx6.value
    bz7=document.form0.resultbx7.value
    bz8=document.form0.resultbx8.value
    bz9=document.form0.resultbx9.value
    }
    /////////////
    Last edited by Azelketh; Jul 30, 2005, 01:59 PM.

  • #2
    Instead of all your ifs you could use a loop like that:
    Code:
    var d = parseInt(d), i = 0;
    for(; i < d; i++) {
    	document.form0['resultbx' + i].value = rond[i];
    }
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

    Comment


    • #3
      Why do you define d as a number then test its value as a string? By way of explanation, when you go:

      var d=0

      you are defining it as a number. If you go:

      var d="0"

      you are defining it as a string. If you test:

      if (d=="4")

      you are testing it as a string, if you use:

      if (d==4)

      you are testing it as a number.
      - John

      Comment


      • #4
        thanks jscheuer1 , i didn't realise that.
        what dumpfi suggested unfortunatly didn't work, thanks anyway.

        Comment


        • #5
          Well, I've just now been trying to get your code to do something and have been having no luck getting it to anything except total up some numbers. You said it was sort of working. Can you give us the rest of the markup so we can test it out?
          - John

          Comment


          • #6
            i've managed to get it to work correctly now,
            again thanks for the help.

            Comment

            Working...
            X