0

我的目标:

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

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

6 回答 6

2

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

于 2013-02-26T19:23:30.040 回答
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,那么这将是一个无限循环。但是,希望这是一个很好的起点。

于 2013-02-26T19:22:38.873 回答
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;
    }
}
于 2013-02-26T19:25:05.513 回答
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;
 }
于 2013-02-26T19:21:40.760 回答
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;
    }
}
于 2013-02-26T19:22:26.267 回答
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;
于 2013-02-26T19:27:58.057 回答