[英]Removing random element from an array /Javascript
我用谷歌搜索了問題,但沒有找到答案,在此先感謝您的幫助。 問題是,我有一些可以正常運行的代碼,但我想對其進行改進:
function go(times) {
function pick(n) {
return n[Math.floor(Math.random() * n.length)];
}
var body = ["face", "nose", "hair", "smile"];
var adj = ["amazing", "nice", "beautiful", "perfect"];
var word = ["great", "innocent", "glowing", "adorable"];
var str = "Your " + pick(body) + " looks " + pick(adj) + " and " + pick(word) + "!";
if (times > 0) {
for (i = 0; i < times; i++) {
str = str + " And " + go().toLowerCase();
}
}
return str;
}
選擇隨機詞后,應將其從數組中刪除,這樣就不會重復。 如果我知道元素的確切索引,則可以使用splice函數來處理它,但是當它是隨機的時,我不希望它起作用。
您可以輕松地向所有數組添加函數以返回隨機值和/或隨機刪除一個值。
// After this, you can call.getRandomValue() on any array!
Array.prototype.getRandomValue = function(removeItem) {
if (this.length < 1) throw "Cannot get random value from zero-length array";
var randomIndex = Math.floor(Math.random() * this.length);
var randomValue = this[randomIndex];
if (removeItem)
this.splice(randomIndex, 1);
return randomValue;
};
function constructDescription(sentenceCount) {
var body = ["face", "nose", "hair", "smile"];
var adj = ["amazing", "nice", "beautiful", "perfect"];
var word = ["great", "innocent", "glowing", "adorable"];
var description = "";
for(var i = 0; i < sentenceCount; i++) {
if (body.length > 0 && adj.length > 0 && word.length > 0) {
description += (description.length > 0) ? " And your " : "Your ";
description += body.getRandomValue(true) + " looks " + adj.getRandomValue(true) + " and " + word.getRandomValue(true) + "!"
}
}
return description;
}
您只需要結合您的接頭和您的隨機發生器。 例:
function go(times) {
var body = ["face", "nose", "hair", "smile"];
var adj = ["amazing", "nice", "beautiful", "perfect"];
var word = ["great", "innocent", "glowing", "adorable"];
function pick(n) {
return n.splice(Math.floor(Math.random() * n.length), 1);
}
var str = "";
for (var i = 0; i < times; i++) {
str += (i > 0 ? " And your ":"Your ") + pick(body) + " looks " + pick(adj) + " and " + pick(word) + "!";
}
return str;
}
使用其他函數,而不是在循環中遞歸調用go()
。 通過為每個短語調用go()
,您每次都會初始化原始數組。 然后在pick()
進行拼接
function go(times) { var body = ["face", "nose", "hair", "smile"]; var adj = ["amazing", "nice", "beautiful", "perfect"]; var word = ["great", "innocent", "glowing", "adorable"]; var str = '' function pick(n) { var idx = Math.floor(Math.random() * n.length); var str = n[idx]; n.splice(idx, 1) return str; } function getPhrase(i) { var phrase = pick(body) + " looks " + pick(adj) + " and " + pick(word) + "!"; return i == 0 ? "Your " + phrase : " And your " + phrase; } for (var i = 0; i < times; i++) { str += getPhrase(i); } return str; } document.body.innerHTML = go(4);
@lucounu解決方案絕對正確。
如果您只是想改善最初的解決方案,則可以執行以下操作:
var body = ["face", "nose", "hair", "smile"];
var adj = ["amazing", "nice", "beautiful", "perfect"];
var word = ["great", "innocent", "glowing", "adorable"];
function go(times) {
function pick(n) {
var index = Math.floor(Math.random() * n.length)
var randomString = n[index];
n.splice(index,1);
return randomString;
}
var str = "Your " + pick(body) + " looks " + pick(adj) + " and " + pick(word) + "!";
if (times > 0) {
for (i = 0; i < times; i++) {
str = str + " And " + go().toLowerCase();
}
}
return str;
}
console.log(go(2));
試試看
var data = ["brain", "mitochondria", "microsope", "beaker", "beaker-2", "scientist", "cell", "atom"];
while (data.length) {
document.write(data.splice(data.length * Math.random() | 0, 1)[0] + '<br>');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.