gpt4 book ai didi

Javascript - 选择随机数组 IF 与当前数组相同,然后选择另一个

转载 作者:行者123 更新时间:2023-11-28 16:05:37 26 4
gpt4 key购买 nike

我的目标:

我正在尝试在我的网站上创建一个小部分用于推荐。

我有 1 个感言,单击按钮后,当前感言消失,框中出现一个新的随机感言。这很好用。但...我注意到随机选择器抛出了重复的推荐(显示了推荐 1,单击了按钮,并且仍然偶然出现了推荐 1)

我正在尝试编写一个命令:如果新数组与前一个数组相同,则重复随机选择过程(重做数学)否则写(innerHTML)新的推荐。

我的问题是我不知道 IF 部分的编码(我在其中潦草地写着“与当前消息相同”)

下一阶段将是“转到脚本开头”部分(重做数学)

如果有人能在这里帮助我,我真的很感激,因为我有点无知!

提前谢谢

function quotes() {
//Define and populate the array
var aquote = new Array;
aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it. My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
aquote[2] = "\"Thank you for our wedding cake. The fruit cake was absolutely delicious and so moist. The flowers you made were beautiful and exactly as we imagined they would be. We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz – Desborough"

//Generate a random number then print the quote from that array index
rdmQuote = Math.floor(Math.random() * aquote.length);
if (rdmQuote = aquote[SAME AS CURRENT MESSAGE]) {
alert('quote is same as current')
} else {
document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
}
}

最佳答案

我将定义该数组,使其仅初始化一次。然后我会选择一个介于 0 和 n-2 之间的随机条目 m(其中 n 是数组大小),并将第 m 个条目与第 n-1 个条目交换并显示该条目。因此,新的选择永远无法选择当前显示的条目。

关于Javascript - 选择随机数组 IF 与当前数组相同,然后选择另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15097388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com