Javascript-选择与当前数组相同的随机数组IF,然后选择另一个
Javascript - Pick random array IF same as current array then pick another
我的目标:
我正试图在我的网站上创建一个小部分,用于推荐。
我有1个推荐信,当点击按钮时,当前推荐信消失,一个新的随机推荐信出现在框中。这很好用。但是我注意到随机选择器抛出了重复的推荐(显示了推荐1,点击了按钮,推荐1仍然偶然出现(
我正试图写一条命令,上面写着:如果新数组与前一个数组相同,则重复随机选择过程(重新计算(ELSE编写(innerHTML(新的推荐信。
我的问题是我不知道IF部分的编码(我在那里潦草地写了"与当前消息相同"(
此外,下一阶段将是"开始脚本"部分(重做数学(
如果有人能在这里帮我,我会非常感激,因为我有点无知,TBH!
提前感谢
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个条目交换并显示该条目。因此,新的选择永远不能选择当前显示的条目。
存储最后一个报价的索引并进行比较。
jsFiddle示例
var lastQuote = -1;
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
var rdmQuote = Math.floor(Math.random() * aquote.length);
if (rdmQuote == lastQuote) {
alert('quote is same as current')
} else {
document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
lastQuote = rdmQuote;
}
}
一种方法是将元素的内容与随机选择的新内容进行比较,并使用循环选择一个新内容,直到它们不同。
var currentContent = document.getElementById("randomtestimonial").innerHTML;
do {
rdmQuote = aquote[Math.floor(Math.random()*aquote.length)];
} while(currentContent == rdmQuote);
document.getElementById("randomtestimonial").innerHTML = rdmQuote;
这个代码可以改进,因为如果aquote.length
曾经是1,那么这将是一个无限循环。但是,希望这是一个好的起点。
我只缓存当前选择。
<!-- Hide the script from old browsers //-->
var _cache="";
function quotes(){
//Define and populate the array
var aquote = new Array;
// all your quotes
//Generate a random number then print the quote from that array index
rdmQuote = Math.floor(Math.random()*aquote.length);
if (_cache == rdmQuote)
{
alert('quote is same as current')
}
else
{
document.getElementById("randomtestimonial") .innerHTML=aquote[rdmQuote];
}
_cache = rdmQuote;
}
只需使用一个变量来存储当前报价索引。
var current = -1;
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 == current) {
alert('quote is same as current')
} else {
document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
current = rdmQuote;
}
}
你的工作比你的问题标题所暗示的要容易。您希望从数组中随机选择一个字符串,如果它与当前串相同,请选择另一个。
你已经知道如何访问这个字符串,太,做
rdmQuote = Math.floor(Math.random() * aquote.length);
if (rdmQuote == document.getElementById("randomtestimonial").innerHTML]) {
alert('quote is same as current')
} else {
document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
}
注意,我用的是双等号,而不是你用的单等号是赋值。==和===是相等比较。这仍然给你留下了一个问题,如果是一样的,该怎么办(除了提醒(。您需要do/while
控制命令。
var quote = "";
do {
rdmQuote = Math.floor(Math.random() * aquote.length);
quote = aquote[rdmQuote];
} while (quote == document.getElementById("randomtestimonial").innerHTML;
document.getElementById("randomtestimonial").innerHTML = quote;
- Jquery 读取编号组中选择下拉列表的数组
- Javascript:从数组中的对象中选择属性
- Javascript没有从数组中选择背景颜色
- 如何从数组中选择特定标记
- 使用JavaScript或jQuery检查单选按钮选择是否与数组中的项目匹配
- jquery从多维数组中进行动态相关选择
- 如何在javascript中使用click函数选择数组元素
- 构建HTML选择字段并使用JavaScript数组选择选项
- 将增量计数器连接到 JSON 响应循环中的数组选择器的末尾
- 使用 jQuery 使用 id 数组选择元素
- Jquery循环数组选择器与自定义元素ID的
- 转换后的 JSON 数组选择
- 对象数组-选择数组子集,其中对象属性为值数组
- knockoutjsforeach数组选择项值绑定
- 数组选择中的Javascript对象
- 按属性值的数组选择器函数
- 警告JS数组选择
- Javascript 和 HTML mix(数组选择)
- 作为jquery变量的值数组选择器
- 统一更新数组选择值