I'm working on a project that combines a few different loves of mine. D&D, Spreadsheets (Google) and code.
I have this custom function that I am working on, that basically should automatically look up this table:
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| d100 | Result |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 01 - 60 | The item communicates by transmitting emotion to the creating carrying or wielding it. |
| 61 - 90 | The item can speak, read, and understand one or more Languages. |
| 91 - 100 | The item can speak, read, and understand one or more Languages. In addition, the item can communicate telepathically with any character that carries or wields it. |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Here is the script that I have so far:
function communication(max){
for (var roll = Math.floor(Math.random() * Math.floor(max) + 1); 60 > roll; )
return "The item communicates by transmitting emotion to the creating carrying or wielding it.";
for (; 90 > max; )
return "The item can speak, read, and understand one or more Languages.";
for (; 100 > max; )
return "The item can speak, read, and understand one or more Languages. In addition, the item can communicate telepathically with any character that carries or wields it.";
if (typeof something === "undefined") {
Logger.log("something is undefined");
Logger.log(roll)
}
}
It works, mostly. However on occasion it throws out a undefined error, and I can't figure out why.
[18-11-02 21:12:50:909 GMT] something is undefined
[18-11-02 21:12:50:910 GMT] 93.0
[18-11-02 21:12:50:910 GMT] undefined
I know that the last undefined is because I'm not returning the roll variable, but the issue happens even with that returned. 93 is less than 100, and greater than 91 and so that should return:
The item can speak, read, and understand one or more Languages. In addition, the item can communicate telepathically with any character that carries or wields it.
Anyone able to shed some light on it?
I'd suggest a full rewrite of this code. You need to stop using for
statements in place of if
.
function c(max) {
var roll = Math.floor(Math.random() * Math.floor(max) + 1)
if (roll <= 60)
return "The item communicates by transmitting emotion to the creating carrying or wielding it.";
else if (roll <= 90)
return "The item can speak, read, and understand one or more Languages.";
else if (roll <= 100)
return "The item can speak, read, and understand one or more Languages. In addition, the item can communicate telepathically with any character that carries or wields it.";
}