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

Javascript - Pick random array IF same as current array then pick another

本文关键字:数组 选择 IF 随机 然后 另一个 Javascript-      更新时间:2023-09-26

我的目标:

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

我有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;