不确定的东西,我不知道为什么

时间:2018-11-02 21:36:07

标签: javascript google-apps-script ecmascript-5

我正在从事一个结合了我的一些不同爱好的项目。 D&D,电子表格(Google)和代码。

我有一个正在使用的自定义函数,基本上应该会自动查找此表:

+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|   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]某些内容未定义
  [18-11-02 21:12:50:910 GMT] 93.0
  [18-11-02 21:12:50:910 GMT]未定义

我知道最后一个未定义的原因是我没有返回roll变量,但是即使返回了该变量,该问题也会发生。 93小于100,并且大于91,因此应返回:

  

该项目可以说,阅读和理解一种或多种语言。在   此外,该物品可以与任何角色进行心灵感应交流   携带或使用它。

有人能对此有所了解吗?

2 个答案:

答案 0 :(得分:1)

我建议完全重写此代码。您需要停止使用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.";
 }

答案 1 :(得分:1)

所以,我很傻,以一种不好的方式使用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;
}