【发布时间】: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 customColorsArray是string而不是array。正确的?customColorsArray[Math.floor(Math.random() * customColorsArray.length)]; };与"whatever"[1]相同
标签: google-apps-script google-sheets return return-value spreadsheet