Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone look at this code?

Status
Not open for further replies.

adninja

Programmer
May 11, 2005
11
US
//Dice Button
function tag_dice()
{
var FoundErrors = '';
var Num = prompt("Please enter the number of dice", "1");
var Sides = prompt("Please enter the number of sides","2");
var Mod = prompt ("Please enter any modifiers","0");
var a = (Num*Sides)-Num;
var Rand = Math.random();
var b = Rand*a;
var Roun = Math.round(b);
var c = Number (Num) + Number (Mod);
var Roll = Number (Roun) + Number (c);

if (!Num) {
FoundErrors += "No dice?"
}

if (!Sides) {
FoundErrors += "That die cannot exist."
}

if (!Mod) {
FoundErrors += "That does not work you idiot!"
}

if (FoundErrors) {
alert("Error! "+FoundErrors);
return;
}
if (Mod==0) {doInsert(Num+"d"+Sides+": "+Roll);}
else if (Mod>0) {doInsert (Num+"d"+Sides+"+"+Mod+": "+Roll);}
else if (Mod<0) {doInsert (Num+"d"+Sides+Mod+": "+Roll);}

}
if (dice >= userLV){
addButton("<input type='button' class='codebuttons' value='Dice' onmouseover='descript(\"Dice Roll (alt+r)\")' onclick='tag_dice()' accesskey='r' style='font-size: 10px' //> ");
temp = line(temp,ButtonCount,place);
}

I have this stored in a js sourcefile. Is there any way for the doInsert value to be readonly?
 

What do you mean by "the doInsert value"? doInsert looks like a function.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I just started learning javascript yesterday afternoon so I don't think I can address your question, but I thought I might mention a flaw in your 'rolling' algorithm. You have to perform each die roll separately otherwise the distribution will be fairly linear when it should be a bell curve (if you want to see this, compare the number of results for each number 2 to 12 you get when rolling two six-sided die) The code to generate your number should look something like this:
var a =0;
for (ctr1=0;ctr1<Num;ctr1++)
a += Math.round(Math.random() * Sides);
a+=Mod;

also, I think this line:
else if (Mod<0) {doInsert (Num+"d"+Sides+Mod+": "+Roll);
has a flaw, if Mod is negative you want it to display as a negative without the '+' so it will look like "3d5-2: 10" but instead you get "3d3: 10" you need to throw a blank string in between them so they do not add together
it should look something like this I think:
else if (Mod<0) {doInsert (Num+"d"+Sides+""+Mod+": "+Roll);
this might actually be your problem.

Best of luck.

Jaa
 
Actually, when it comes to the problem with the line:
else if (Mod<0) {doInsert (Num+"d"+Sides+Mod+": "+Roll);

The result is "3d5-2: 10" because the + sign just tells the script that it is part of a string of values. THe only way for the script to add them together (I learned this the hard way when I was learning last week [I am also very new when it comes to this]) is to put the container Number () around the values. Number (5) + Number (-2) is 3, but 3+-2 is 3-2. Weird.

And when I ask about the doInsert values, I mean the text that is displayed when the command is executed (remember, I am new and don't know all of the jargon yet). I just want the text to be displayed and the person writing in the textarea of my forum cannot edit the results. It would be even better if it looked "disabled" (the text was a light gray or some other color the member cannot do manually) in order to denote a legit roll.
 
So what you really want is to make a textarea read only? In which case, I'll refer you to your previous post where the exact answer was given by Jeff.

thread216-1059027

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Actually, the reaonly should not affect the rest of the textarea, but instead that one "2d4+2: 9" or whatever they rolled.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top