有些东西是未定义的,我不明白为什么
Something is undefined, and I can't figure out why
我正在从事一个结合了我的一些不同爱好的项目。 D&D、电子表格 (Google) 和代码。
我正在处理这个自定义函数,基本上应该会自动查找这个 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. |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
这是我目前拥有的脚本:
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)
}
}
大部分情况下都有效。但是有时它会抛出一个未定义的错误,我不明白为什么。
[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
我知道最后一个未定义是因为我没有 returning roll 变量,但即使 returned 也会出现问题。
93 小于 100,大于 91,所以应该 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.
有人能解释一下吗?
我建议完全重写这段代码。您需要停止使用 for
语句来代替 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.";
}
所以,我很傻,并且以错误的方式使用 for 语句。
这实现了我的目标:
function communication(max) {
var roll = Math.floor(Math.random() * Math.floor(max) + 1);
if (60 > roll)
return "The item communicates by transmitting emotion to the creating carrying or wielding it.";
return 90 > roll
? "The item can speak, read, and understand one or more Languages."
: 100 > roll
? "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."
: void 0;
}
我正在从事一个结合了我的一些不同爱好的项目。 D&D、电子表格 (Google) 和代码。
我正在处理这个自定义函数,基本上应该会自动查找这个 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. |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
这是我目前拥有的脚本:
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)
}
}
大部分情况下都有效。但是有时它会抛出一个未定义的错误,我不明白为什么。
[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
我知道最后一个未定义是因为我没有 returning roll 变量,但即使 returned 也会出现问题。 93 小于 100,大于 91,所以应该 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.
有人能解释一下吗?
我建议完全重写这段代码。您需要停止使用 for
语句来代替 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.";
}
所以,我很傻,并且以错误的方式使用 for 语句。
这实现了我的目标:
function communication(max) {
var roll = Math.floor(Math.random() * Math.floor(max) + 1);
if (60 > roll)
return "The item communicates by transmitting emotion to the creating carrying or wielding it.";
return 90 > roll
? "The item can speak, read, and understand one or more Languages."
: 100 > roll
? "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."
: void 0;
}