【发布时间】:2018-07-06 10:13:05
【问题描述】:
我收到了邮箱中的脑筋急转弯,这应该需要 20 分钟,但显然,我卡在了 Chrome 崩溃的范围内。这个想法是为您提供一个字符串。然后,您可以使用该字符串生成类似于 lorum ipsum 的随机句子。
var words = "The sky above the port was the color of television, tuned to a
dead channel. All this happened, more or less. I had the story, bit by bit,
from various people, and, as generally happens in such cases, each time it
was a different story. It was a pleasure to burn.";
var wordList = words.split(' ');
var numWords = getRandomInt(2, 8);
var numSentinces = getRandomInt(8, 40);
var sentinces = [];
var sentince = [];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function genSentinces() {
while (numWords > 0) {
sentince.push(wordList[getRandomInt(0, wordList.length)]);
numWords--;
}
sentince = sentince.join(' ');
console.log(sentince)
return sentince;
}
genSentinces();
genSentinces();
我假设语句变量的范围是错误的,因为它第一次运行但不是第二次。我想我需要在某处添加这个。 任何帮助将不胜感激,因为我可以阅读其中包含此内容的代码,但我显然还无法使用此代码编写代码。
【问题讨论】:
-
在您的代码中,将
sentince.push更改为sentinces.push,并与赋值右侧的 join 语句相同。
标签: javascript scope this