Javascript 有些东西没有定义,我可以';我不明白为什么

Javascript 有些东西没有定义,我可以';我不明白为什么,javascript,google-apps-script,ecmascript-5,Javascript,Google Apps Script,Ecmascript 5,我正在做一个结合了我的一些不同爱好的项目。D&D、电子表格(谷歌)和代码 我有一个我正在使用的自定义函数,基本上应该自动查找此表: +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | d1

我正在做一个结合了我的一些不同爱好的项目。D&D、电子表格(谷歌)和代码

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

+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|   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-0221:12:50:909 GMT]有些东西是未定义的
[18-11-0221:12:50:910GMT]93.0
[18-11-0221:12:50:910GMT]未定义

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

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


有人能解释一下吗?

我建议完全重写这段代码。您需要停止使用
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.";
 }
功能c(最大值){
var roll=Math.floor(Math.random()*Math.floor(max)+1)

如果(roll是这样的话,我就是在愚蠢地使用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;
}

for(;90>max;)
您可能在此处查找
if
。例如
if(max>90)
等等。你为什么要用这样的语句?你首先在哪里定义某个东西?当然,如果你不在范围内定义它,它将是未定义的。你们把我放在正确的轨道上。有一个有效的答案,并将其作为答案发布在下面。谢谢,我知道我很愚蠢!如果你看@stewartmcgown的答案,它是一个l虽然我会在他的回答中指出一个小错误,但这并不容易理解发生了什么。将这里的
if
三元混用看起来就像你试图让人们更难理解。:)