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

时间:2013-02-26 19:15:40

标签: javascript random repeat

我的目标:

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

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

6 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

执行此操作的一种方法是将元素的内容与随机选择的新内容进行比较,并使用循环选择新的内容,直到它们不同为止。

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,那么这将是一个无限循环。但是,希望这是一个很好的起点。

答案 2 :(得分:1)

存储最后一个报价的索引并进行比较。

<强> jsFiddle example

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;
    }
}

答案 3 :(得分:0)

我只是缓存当前的选择。

  <!-- 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;
 }

答案 4 :(得分:0)

只需使用变量来存储当前报价索引。

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;
    }
}

答案 5 :(得分:0)

您的工作比问题标题更容易。您希望从数组中选择一个随机的字符串,如果它与当前的字符串相同,请选择另一个。

您已经知道如何访问此字符串,正在执行

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;