2015-12-06 70 views
0

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

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?任何其他方式来做到这一点?

+0

[从阵列获得的随机值]的可能的复制(http://stackoverflow.com/questions/4550505/getting-random - 值从 - 一个阵列)。 < - 只需做1000次,将每个结果推送到一个数组,然后在“”上加入它,或者简单地追加到每个迭代上的字符串 – Phil

+1

(for var i = 1000; i--;)document.body.innerHTML + = words [Math.floor(Math.random()* words.length)];' – adeneo

回答

1

这将需要以随机的顺序所有你的话:

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)

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

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

+0

我认为你应该在单词之间加空格。 – CoderPi

+0

@CodeiSir完成! –

+1

不错。所以你的答案会提供一个“句子”,其中包含多个词语。而我的答案只会使用每个单词一次。已完成的答案 – CoderPi

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

至于我个人喜欢Lorem存有,这个怎么样?

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(); 

Google文档培根存有(唯一存有我知道,让CORS请求):http://baconipsum.com/json-api/

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())

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);