David FoxFire
Adventurer
- Xy$
- 0.00
I know that this isn't quite a Javascript programming group, but I don't know where else to put this. I've been dabbling in Javascript code and I want to know if I got it right. I designed my first Javascript Formula that does a D&D 5th Edition d20 roll, with Advantage or Disadvantage (for the uninitiated, that's when you roll two d20s and take the higher or lower result) adds a modifier and returns the modifier, or "Nat Crit!" when the result is a Natural 20.
I just want to know if I have the syntax right. I'm not concerned about compressing code or professional level scripting, I just want the scripts I write to a) work, and b) be readable enough for me to know what it's doing.
function Roll20 (modifier, advantage) {
var a-roll = Math.floor((Math.random()*20)+1);
var b-roll = Math.floor((Math.random()*20)+1);
var die-result = 0;
switch (advantage){
case -1:
if (a-roll < b-roll){
die-result = a-roll;
}
else {
die-result = b-roll;
};
break;
case 0:
die-result = a-roll;
break;
case 1:
if (a-roll > b-roll){
die-result = a-roll;
}
else {
die-result = b-roll;
};
};
if die-result === 20 {
return ("Nat Crit!");
}
else{
return (die-result + modifier);
}
}
Your assistance and input is greatly appreciated. Thanks in advance.
I just want to know if I have the syntax right. I'm not concerned about compressing code or professional level scripting, I just want the scripts I write to a) work, and b) be readable enough for me to know what it's doing.
function Roll20 (modifier, advantage) {
var a-roll = Math.floor((Math.random()*20)+1);
var b-roll = Math.floor((Math.random()*20)+1);
var die-result = 0;
switch (advantage){
case -1:
if (a-roll < b-roll){
die-result = a-roll;
}
else {
die-result = b-roll;
};
break;
case 0:
die-result = a-roll;
break;
case 1:
if (a-roll > b-roll){
die-result = a-roll;
}
else {
die-result = b-roll;
};
};
if die-result === 20 {
return ("Nat Crit!");
}
else{
return (die-result + modifier);
}
}
Your assistance and input is greatly appreciated. Thanks in advance.