Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 随机测试年选择_Javascript_Arrays_Random - Fatal编程技术网

Javascript 随机测试年选择

Javascript 随机测试年选择,javascript,arrays,random,Javascript,Arrays,Random,我正在一个网站上进行随机评论/测试。 我使用JS将索引页面onLoad上的testomonial随机化。在我的代码中,我使用一个数组来存储testomonial,然后当页面加载时,我希望它从数组中随机选择3条评论,并将它们分别写入“review-1”、“review-2”和“review-3” 我在代码中遇到的问题是,idk是选择3个不同评论的最佳方法,而不会重复同一个评论两次 var reviews = [ "Thank you Anne for fitting me in

我正在一个网站上进行随机评论/测试。 我使用JS将索引页面onLoad上的testomonial随机化。在我的代码中,我使用一个数组来存储testomonial,然后当页面加载时,我希望它从数组中随机选择3条评论,并将它们分别写入“review-1”、“review-2”和“review-3”

我在代码中遇到的问题是,idk是选择3个不同评论的最佳方法,而不会重复同一个评论两次

 var reviews = [
    "Thank you Anne for fitting me in yesterday when you realised I was desperate to get the house cleaned before the blinds and curtains were fitted. Marie and Michaela did a great job, leaving it sparkling clean. I will certainly recommend you to anyone who needs a cleaner. That you are so approachable, helpful and friendly is a bonus. - <strong>Rosemary OBoyle</strong>",
    "Great job on all the awkward hate to do Jobs! Came home from my holidays and my house was sparkling, highly recommended!! - <strong>Lynne Gardiner</strong>",
    "Domestic Angels are angels to me, just left lovely Kelly cleaning my house in preparation for mums arrival, while I chill at hairdressers, thank you to Anne & her team, can\'t recommend them enough - <strong>Julie Magee</strong>"
]

var max = reviews.length;
var id1;
var id2;
var id3;

function getRandomReview(max) {
    id1 = Math.floor(Math.random() * Math.floor(max));
    id2 = Math.floor(Math.random() * Math.Floor(max));
    id3 = Math.floor(Math.random() * Math.Floor(max));
}

function randomJS() {
    getRandomReview(max);
    document.getElementById("review-1").innerHTML = id1;
    document.getElementById("review-2").innerHTML = id2;
    document.getElementById("review-3").innerHTML = id3;
}
var审查=[
“安妮,谢谢你昨天给我安排了时间,因为你意识到我非常想在百叶窗和窗帘安装好之前把房子打扫干净。玛丽和米凯拉做得很好,让房子闪闪发光。我一定会向任何需要清洁工的人推荐你。你如此平易近人、乐于助人和友好是一个额外的收获。-罗斯emary OBoyle“,
“在所有讨厌做的尴尬工作上做得很好!我度假回家,我的房子闪闪发光,强烈推荐!!-林恩·加德纳”,
“家庭天使对我来说是天使,刚刚离开可爱的凯莉打扫我的房子,为妈妈们的到来做准备,而我在理发店发冷,谢谢安妮和她的团队,再推荐他们也不够——朱莉·玛吉”
]
var max=0.1.1.1.1.1.2.2.1.2.2.1.2.2.2.1.2.2.2.2.2;
变量id1;
var id2;
变量id3;
函数getRandomReview(最大值){
id1=Math.floor(Math.random()*Math.floor(max));
id2=Math.floor(Math.random()*Math.floor(max));
id3=Math.floor(Math.random()*Math.floor(max));
}
函数randomJS(){
getRandomReview(最大值);
document.getElementById(“review-1”).innerHTML=id1;
document.getElementById(“review-2”).innerHTML=id2;
document.getElementById(“review-3”).innerHTML=id3;
}
任何建议和帮助都将不胜感激。提前谢谢

谢谢

刚刚洗牌,它就开始工作了!:)

var审查=[
“安妮,谢谢你昨天给我安排了时间,因为你意识到我非常想在百叶窗和窗帘安装好之前把房子打扫干净。玛丽和米凯拉做得很好,让房子闪闪发光。我一定会向任何需要清洁工的人推荐你。你如此平易近人、乐于助人和友好是一个额外的收获。-罗斯emary OBoyle“,
“在所有讨厌做的尴尬工作上做得很好!我度假回家,我的房子闪闪发光,强烈推荐!!-林恩·加德纳”,
“家庭天使对我来说是天使,刚离开可爱的凯莉打扫我的房子,为妈妈们的到来做准备,而我在理发店发冷,谢谢安妮和她的团队,再推荐他们也不够——朱莉·玛吉”,
]
var max=0.1.1.1.1.1.2.2.1.2.2.1.2.2.2.1.2.2.2.2.2;
变量id1;
var id2;
变量id3;
职能改组(审查){
reviews.sort(()=>Math.random()-0.5);
}
函数randomJS(){
洗牌(评论);
document.getElementById(“review-1”).innerHTML=reviews[0];
document.getElementById(“review-2”).innerHTML=reviews[1];
document.getElementById(“review-3”).innerHTML=reviews[2];
}

您可以对数组进行洗牌,然后使用从洗牌数组中获得的顺序作为您显示推荐信的顺序。您可能会有3次以上的评论?只是一个警告,众所周知,洗牌方法不是非常随机的。如果您需要更多的随机性,请查看fisher-yates随机洗牌算法,以获得一个好的随机洗牌算法。
var reviews = [
    "Thank you Anne for fitting me in yesterday when you realised I was desperate to get the house cleaned before the blinds and curtains were fitted. Marie and Michaela did a great job, leaving it sparkling clean. I will certainly recommend you to anyone who needs a cleaner. That you are so approachable, helpful and friendly is a bonus. - <strong>Rosemary OBoyle</strong>",
    "Great job on all the awkward hate to do Jobs! Came home from my holidays and my house was sparkling, highly recommended!! - <strong>Lynne Gardiner</strong>",
    "Domestic Angels are angels to me, just left lovely Kelly cleaning my house in preparation for mums arrival, while I chill at hairdressers, thank you to Anne & her team, can\'t recommend them enough - <strong>Julie Magee</strong>",
]

var max = reviews.length;
var id1;
var id2;
var id3;

function shuffle(reviews) {
    reviews.sort(() => Math.random() - 0.5);
}

function randomJS() {
    shuffle(reviews);
    document.getElementById("review-1").innerHTML = reviews[0];
    document.getElementById("review-2").innerHTML = reviews[1];
    document.getElementById("review-3").innerHTML = reviews[2];
}