【问题标题】:Why my return array function won't using or combinning with "Math" methode after it has returnig to the array's values?为什么我的返回数组函数在返回数组值后不会使用或结合“数学”方法?
【发布时间】:2019-12-12 13:05:59
【问题描述】:

猜猜...我正在尝试编写一个函数,该函数返回一个可以传递为字符串格式的数组。就像我将在这里提到的那样:我有 randomColor(customColorsArrays,takingColorsArray) 函数依赖于类已填充 如果它们都是空返回color 的值是通过 Math 方法计算出来的如果 customColorsArrays 的类已经被用户自己填充了来放置自己的 Array 主题如果这两个类都是用户自己写的返回数组的 var 已设置在该函数内部的较早位置。为了清楚地看到这些代码的下方:


function randomColor(customColorsArray, takenColorsArray) {
 var text = "",
     colors = ["orange", "yellow", "red", "maroon"];

   if (customColorsArray && takenColorsArray) {
      var text = "["+colors+"]";
   }
     else if (!customColorsArray && !takenColorsArray) {
      text += colors[Math.floor(Math.random() * colors.length)];
   }
     else {
      text += customColorsArray[Math.floor(Math.random() * customColorsArray.length)];
  };

   return text;
}

function personalRandomColor(e) {
 var text = "";

   if (e == "orange") {text += "almond";}
    else if (e == "yellow") {text += "melrose";}
    else if (e == "red") {text += "danube";}
    else if (e == "maroon") {text += "magenta";};

  return text;
}


这是实现上述功能的 HTML 代码。

bla... bla... bla...
  var customColorsArrays = randomColor('passingClass', 'takenColor'),
      randomFirstColor = randomColor(),
      skipFirstColors = customColorsArrays.replace('\[', '\[\"').replace('\]', '\"\]').replace(/[\,]/g, '\"\, \"').replace('\"'+randomColor()+'\"\,', ''),
      randomSecondColor = personalRandomColor(randomColor(toString(skipFirstColors))),
bla... bla... bla...

【问题讨论】:

  • 预期输出是什么?什么是问题?什么有效,什么无效?见minimal reproducible example
  • @TheMaster ... 为什么在我测试从“randomSecondColor”的 var 运行函数时,例如:console.log(randomSecondColor);输出不是我真正期望的输出是一个字符不是颜色的值???
  • 你好@JamalLudin,你能描述一下你想要完成什么的想法吗?也许这样我们就可以理解问题并为您提供帮助。谢谢!!
  • @carlesgg97 ...我想从 randomSecondColor 的返回中准确选择值,它必须是颜色值而不是输出字符 ...
  • typeof customColorsArraystring 而不是 array。正确的? customColorsArray[Math.floor(Math.random() * customColorsArray.length)]; };"whatever"[1] 相同

标签: google-apps-script google-sheets return return-value spreadsheet


【解决方案1】:

修复代码的一种简单直接的方法是替换该行

randomSecondColor = personalRandomColor(randomColor(toString(skipFirstColors)))

randomSecondColor = personalRandomColor(randomColor(JSON.parse(skipFirstColors)))

原因是randomColor 接受元素数组,而您正在向它传递一个字符串。参数skipFirstColors 是一个字符串,它实际上是字符串列表的 JSON 表示。你希望它是一个实际的列表,而不是一个代表列表的字符串。

JSON.parse 函数将评估由 skipFirstColors 返回的字符串并返回它所代表的列表。

此外,我建议您重新访问并可能重构您的代码。您应该尝试使用内置类型(例如列表)作为函数返回,而不是使用对象的 JSON 表示(序列化为字符串),因为这样可以避免此类错误,并使代码更简洁。

【讨论】:

  • 天哪,谢谢...最后,您已经清楚而明确地回答了我的问题。这也是@TheMaster从一开始就试图向我解释的一个原因,非常感谢你们!其余的你们也猜...谢谢一千次...
  • @jamalludin 很高兴知道它对你有用,伙计! :)
  • 哦,嘿...@carlesgg97 我可以再问你一次,这仍然是关于上面发生的事情!为什么在我的“randomSecondColor's”变量中有时但不会太频繁,它应该会在几次后结束,即使我已经输入代码来替换已加载的内容,它也会显示数组的所有项目在“randomFirstColor”中,然后这将是跳过第一种颜色。 ???
  • 对不起,我自己解决了。我刚刚通过修复 .replace() 行中的一些 regEx 来解决这个问题。
猜你喜欢
  • 2021-02-10
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 2021-06-28
相关资源
最近更新 更多