使用javascript创建虚拟文本

时间:2015-12-06 22:36:01

标签: javascript

我想使用javascript创建1000个单词的虚拟文本。我的想法是使用像这样的数组

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"];

然后随机使用javascript输出1000个单词。我怎么用javascript做到这一点?还有其他方法吗?

6 个答案:

答案 0 :(得分:3)

看看这个



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 text = [];
var x = 1000;
while(--x) text.push(words[Math.floor(Math.random() * words.length)]);
document.write(text.join(" "))




参考: SO | Getting random value from an array

答案 1 :(得分:2)

    var m = words.length,
    t, i,result=[];
while (m && result.length < 100) {
    i = Math.floor(Math.random() * m--);
    t = arr[m];
    arr[m] = arr[i];
    arr[i] = t;
    result.push(arr[m]);
}

答案 2 :(得分:2)

我个人喜欢Lorem Ipsum,这个怎么样?

var r = new XMLHttpRequest();
r.onload = function() {
    if (r.readyState != 4 || r.status != 200) return;   // bad query or something...
    document.getElementById('LIpsum').textContent = r.responseText;
};
r.open('GET', 'https://baconipsum.com/api/?type=meat-and-filler&paras=18&format=text');
r.send();

关于Bacon Ipsum的文档(我知道允许CORS请求的唯一Ipsum):http://baconipsum.com/json-api/

答案 3 :(得分:1)

这将以随机顺序记录您的所有单词:

&#13;
&#13;
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

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"];

shuffle(words);
var sentence = words.join(" ")

// Demo
console.log(words);
console.log(sentence);
document.write(sentence)
&#13;
&#13;
&#13;

使用How to randomize (shuffle) a JavaScript array?

中的shuffle()

答案 4 :(得分:1)

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"];

function randomSentence() {
  var n = 1000;
  var sentence = "";
  while (n--) {
    sentence += words[Math.floor(Math.random() * words.length)] + " ";
  }
  return sentence;
}
document.writeln(randomSentence())

答案 5 :(得分:1)

我将标点符号和大小写分开,并计算单词,而不是数组中使用的项目。这是一个jsfiddle:

https://jsfiddle.net/eexLwt4L/1/

以下是代码:

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"],
    punctuation = [".", ","],
    text = "",
    phrase,
    punc,
    count = 0,
    nextCapital = true;
while(count<1000) {
  phrase = words[Math.floor(Math.random() * words.length)]
  text += nextCapital ? phrase[0].toUpperCase() + phrase.slice(1): phrase;
  nextCapital = false;
  if ( Math.random() > .8 ) {
    punc = punctuation[Math.floor(Math.random() * punctuation.length)];
    if ( punc === "." ) nextCapital = true;
    text += punc;
  }
  text += " ";
  count = text.match(/\S+/g).length;
}
document.write(text);