【问题标题】:On each submit to show a new random array class of random array subclass在每次提交时显示一个新的随机数组类的随机数组子类
【发布时间】:2019-02-24 19:45:05
【问题描述】:

我正在创建一个 lorem ipsum 应用程序,我想单击一个按钮以从随机字符生成随机引号。并且字符之间不会混合引号​​。但是,每次我单击提交时,它只会显示我在数组中列出的第一个字符的随机引号。我该如何解决这个问题?

const aQuotes = require("./public/quotes/a");
const bQuotes = require("./public/quotes/b");
const cQuotes = require("./public/quotes/c");

const loremIpsum = new GenerateNewText();
function GenerateNewText(){}

GenerateNewText.prototype.getRandomSentence = function() {
  const charQuotes =
  [
    aQuotes,
    bQuotes,
    cQuotes,
  ]
  for(var i = 0; i < charQuotes.length; i++){
    let randomSentence = charQuotes[i][Math.floor(Math.random() * charQuotes[i][Math.floor(Math.random())].length)]
    return randomSentence;
  }
}

当我运行上面的示例时,它将显示存储在aQuotes 中的随机引用列表,其中包含“未定义”一词。如果我将bQuotes 移动到数组的顶部,那么它将仅显示随机的bQuotes,同时带有“未定义”一词。为什么它只显示数组中第一个元素的结果,为什么显示“未定义”(即aQuotes)?

const aQuotes = [
    "Lalalalalalalala.",
    "Blah blah blah blah.",
    "Blank Blank Blank Blank Blank." 
]

module.exports = aQuotes;

我尝试做 charQuotes[i][Math.floor(Math.random())].length * charQuotes[i][Math.floor(Math.random())].length 认为它会首先随机化 charQuotes 数组,然后随机化单个 a/b/cQuotes 数组,但它最终返回了一个数字 169 的块。我尝试过的其他“修复”结果在所有undefined 文本的段落中,所有NaN 的段落中,或显示所有字符的所有引号,并在此处和此处插入单词“未定义”。

如何在每次点击时随机化 charQuotes 数组和我的 a/b/cQuotes 数组中的内容?并摆脱出现的奇怪的“未定义”文本?

我正在使用 Node 和 Express。

【问题讨论】:

  • “但是,每次我点击提交时,它只会显示我在数组中列出的第一个字符的随机引号。”i0 时,值为从return randomSentence;的函数返回
  • 好的,我明白你的意思并设置它而不是for(var i; i &lt; charQuotes.length; i++){ let randomSentence = charQuotes[Math.floor(Math.random() * charQuotes[Math.floor(Math.random())].length)] return randomSentence; },但我只是得到未定义的块。

标签: javascript arrays random


【解决方案1】:

您可以.map()charQuotes 并从每个输入数组返回一个伪随机值,然后.sort() 得到伪随机数组

const getRandomSentence = () => {
  const charQuotes =
  [
    ["a", "b", "c"],
    [1, 2, 3],
    [1.5, 2.5, 3.5],
  ];
  const r = charQuotes.map(arr => arr[Math.floor(Math.random() * arr.length)])
            .sort(_ => 0.5 - Math.random());
  return r.join(" ")
}

console.log(getRandomSentence());

【讨论】:

  • 您好,这行得通,但是当我只想要一个字符的引号时,它再次显示每个字符的引号,但一个字符是随机选择的。
  • “一个字符的引号”是什么意思?来自单个数组?
  • 请参阅我在雨果席尔瓦的回答中评论的澄清和哈利波特示例。
  • @TJS13 虽然了解了功能的要点,但我不熟悉提到的任何角色(没有看过这些电影)。鉴于问题中的代码没有数组名称,数组只是数组charQuotes 中的元素(索引)。因此,您可以选择应该映射哪个索引,并在答案和括号符号中使用相同的代码来选择适当的或随机的索引const r = charQuotes[index].map()const r = charQuotes[Math.floor(Math.random() * charQuotes.length)].map()。要为数组命名引用,您可以使用对象
【解决方案2】:

您不需要for 循环。您进入一个循环只是为了在第一次迭代中返回。这就是为什么你总是得到第一个引号数组的原因。

GenerateNewText.prototype.getRandomSentence = function() {
  const allQuotes =
  [
    aQuotes,
    bQuotes,
    cQuotes,
  ]
  const characterQuotes = allQuotes[ Math.floor(Math.random() * allQuotes.length) ]
  return characterQuotes[ Math.floor(Math.random() * characterQuotes.length) ]
}

如果要从同一个字符生成引号,则需要将字符信息保存在函数范围之外的变量中,或者像以下示例一样保存在对象实例中:

GenerateNewText.prototype.allQuotes = [
  aQuotes,
  bQuotes,
  cQuotes,
]
GenerateNewText.prototype.getRandomSentence = function() {
  if ( !this.characterQuotes ) {
    this.characterQuotes = this.allQuotes[ Math.floor(Math.random() * allQuotes.length) ]
  }
  return this.characterQuotes[ Math.floor(Math.random() * characterQuotes.length) ]
}

【讨论】:

  • 这有效,但它返回每个字符的引号,当我希望它只返回一个字符的引号时,但每次单击提交时随机选择一个字符?
  • 对不起,我没有得到你想要的。您想从一个随机字符生成多个引号吗?
  • 假设我们正在讨论生成哈利波特语录。所以我有一个 charQuotes 数组,它们分别链接到 Harry、Hermione 和 Ron 引用的文件。引号也在一个数组中。有关设置,请参阅我的 OP。我想配置一个系统,每次单击提交时,它都会选择一个随机字符(即哈利、罗恩或赫敏)并显示我在他们的文件中的随机引用。因此,如果选择了 Harry,它将仅显示 Harry 文件中的引号,而不是 Hermione 或 Ron。您提供的当前设置将从所有三个字符生成随机引号。这有意义吗?
  • 确实如此。检查更新。只是为了澄清一下,这个例子使用原型,因为它是你已经拥有的,但我可能会以不同的方式做。我会把事情分解成不同的功能,并酌情调用它们。如果不深入了解您的项目,很难说。我希望这对你有用。
  • 不起作用,但感谢您的尝试和建议。我会去修补一下。
猜你喜欢
  • 2011-02-27
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多