Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Javascript 101 checking

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.
 
};
};

semi colons after } not needed ..

if die-result === 20 should be if (die-result === 20) { blah blah}

a-roll die-result , causes errors and also is a bad habit (no offense) aRoll , roll_One , dieResult are most common ways to define variables

The code itselfs for me in my compiler does not work after making a few minor changes it runs as intended as far as your syntax and and readability goes for a beginner? are promising I dont doubt that even someone with little or no experience in any form of programming would have any trouble reading and understanding what exactly youre trying to accomplish . Keep practicing ..Everyday even if not but a quick study session cause if youre anything like me you start forgetting when and where to call a function etc
 
Top