Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Value being displayed as boolean

Gonna tear my hair out! Why is my script showing the roll  variable as a boolean? It should just be a number from 1 to 10. This was produced with the command !continuum Quick|5|0 I can sendChat("test", roll.toString()) and it appears in the chat as a number. Why doesn't it appear that way in the roll template? Note also that when the check  variable appears in the roll template, it appears as a number, not a boolean. Here's the script: //Continuum rolling on("ready", function() {     log("Continuum rolling script loading."); }); on("chat:message", function(msg) {     if(msg.type == "api" && msg.content.indexOf("!continuum ") !== -1) {         var parameters = msg.content.replace("!continuum ", "");         var skill = parameters.split("|")[0];         var rating = Number(parameters.split("|")[1]);         var modifier = Number(parameters.split("|")[2]);         var gm = msg.content.split("|")[3];         var roll = randomInteger(10);         var check = randomInteger(10);         var result = rating - roll + modifier;         var status;         var output = "";                 if(result >= 0) {             status = "Success";         } else {             status = "Failure";         }         if(roll == 1) {             status = "Automatic Success";         }         if(roll == 1 && check == 1) {             status = "Victory!";         }         if(roll == 10 && check == 10) {             status = "Blunder!";         }         if(roll = rating && check == rating) {             status = "Grace!";         }         if(gm == "gm") {             output += "/w gm ";         }         output += "&{template:default} {{name=Roll vs. " + skill + "}} {{Rating=" + rating.toString() + "}} ";         if(modifier != 0) {             output += "{{Modifier=" + modifier.toString() + "}} ";         }         output += "{{Roll=" + roll.toString() + "}} "         if(status == "Victory!" || status == "Blunder!" || status == "Grace!") {             output += "{{Check=" + check.toString() + "}} ";         }         output += "{{Result=" + result.toString() + "}} {{Outcome=" + status + "}}";         sendChat(msg.who, output);     } });
1461702668
The Aaron
Pro
API Scripter
if(roll = rating && check == rating) { Not sure if this is precisely the issue, but it's a good place to start.  You are probably not intending to assign the value of rating to roll in this if check. In Javascript, you should always use === and !== until you understand enough about javascript to know when you should use == and != (at which point, you'll realize there's never a good reason to do it). I suggest making JSLint (or JSHint) a part of your development process (I have my editor set to run code through it whenever I save).  You can also use Google's Closure Compiler to check code. Also, I highly recommend Javascript: The Good Parts by Douglas Crockford . It's a short book with loads of good information on best practices for Javascript.  I've read it half a dozen times and should probably go read it again. =D
1461702813
The Aaron
Pro
API Scripter
JSHint's output from the above code: |36 col 44 warning  84| Expected a conditional expression and instead saw an assignment. |43 col 21 warning  41| Use '!==' to compare with '0'. |46 col 54 warning  33| Missing semicolon.
1461702989

Edited 1461703141
The Aaron
Pro
API Scripter
In fact, I'm sure that is precisely the error.  Likely the and (&&) operator has higher precedence than the assignment (=) operator, resulting in the following: roll = (rating && (check == rating)) rating probably has a falsy value like 0 , or else check and rating are not convertible to an equal value , hence false is the result of the expression and is assigned to roll . (Edit: Actually, it has to be that check and rating are not convertible to an equal value , otherwise && would have returned the first falsy value, whatever rating was assigned.  Instead, it returns the result of the coercing equality operator, which will be of type boolean.) 
Oh good grief! Stupid equals sign! Thanks for spotting that. It works now.
1461703175
The Aaron
Pro
API Scripter
Yup!  Happy to help!
1461706018

Edited 1461706180
Lithl
Pro
Sheet Author
API Scripter
The Aaron said: In fact, I'm sure that is precisely the error. &nbsp;Likely the and (&&) operator has higher precedence than the assignment (=) operator Logical AND (&&) has a precedence of 6. Equality (==) as well as Inequality, Strict Equality, and Strict Inequality have a precedence of 10. Assignment has a precedence of 3. The only operators with a lower precedence than Assignment are yield, yield*, Spread, and Comma. (Operators with higher precedence are processed first, operators with the same precedence are processed in order.) It should make sense that Assignment has an extremely low precedence value. After all, if Assignment had a higher precedence than Logical AND, for example, code like this would be ridiculous: var isValid = stringsAreValid && numbersAreValid; // With wonky precedence: var (isValid = stringsAreValid) && numbersAreValid; And the operators with lower precedence than Assignment allow for constructs like this: var stringsAreValid = validateStrings(), numbersAreValid = validateNumbers(); <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence</a>