的Javascript - 选择随机数组,如果同目前的阵列,然后选择另一个

问题描述:

我的目标:的Javascript - 选择随机数组,如果同目前的阵列,然后选择另一个

我想创建我的网站推荐的一小部分。

我有1个推荐,当点击按钮时,当前的推荐消失,并在框中出现一个新的随机推荐。这工作正常。 但是...... 我注意到,在随机选择的投掷式两份证言(证言1所示,点击按钮和证明1还是偶然出现)

我想写说的命令: 如果新阵列与先前的阵列相同,则重复随机选择过程(重做数学) 否则写入(innerHTML)新的证明。

我的问题是,我不知道中频部分的编码(在那里我有潦草的“同当前的消息”)

而且下一阶段将是“去启动脚本”部分(重做数学)

我真的很感激,如果有人可以帮助我在这里,因为我是一个无言的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]; 
    } 
} 

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

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

这很好用!谢谢!如果发现重复报价,我将如何获得数学重复? – 2013-02-26 19:34:35

+0

你的意思是如果选择重复报价,你将如何再次运行该功能?只要将if部分改为'if(rdmQuote == lastQuote){ quotes() }' – j08691 2013-02-26 19:43:27

+1

当然!非常感谢! #weightoffmymind – 2013-02-26 19:47:25

我只想缓存当前选择。

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

谢谢,但这似乎并不奏效。不管怎么说,还是要谢谢你。当这个函数被调用时, – 2013-02-26 19:35:09

只需使用一个变量来存储当前报价指数。要做到这一点

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

是不是''''current''''被重置为-1? – GSP 2013-02-26 19:23:45

+0

看看我是否给你你需要的东西。 – 2013-02-26 19:24:58

+0

@GSP谢谢,更新了答案。 – ATOzTOA 2013-02-26 19:25:38

一种方法是将元件与随机选择的新内容的内容比较和使用循环选择一个新的,直到他们是不同的。

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

+0

谢谢。这似乎仍然在抛出重复。 – 2013-02-26 19:33:00

+0

@MichaelWalkling这意味着对document.getElementById(“randomtestimonial”)。innerHTML'''的调用返回一个不同于“'aquote [Math.floor(Math.random ()* aquote.length)]''''。 – GSP 2013-02-26 19:38:50

+0

@MichaelWalkling一个建议(如果你还没有使用它)使用FireFox中的FireBug来帮助诊断这些JavaScript问题。这是一个很棒的工具。在这种情况下,调用''''console.log(“”+ document.getElementById(“randomtestimonial”)。innerHTML +“vs”+ aquote [Math.floor(Math.random()* aquote.length)] );循环内部的''''会很快显示你正在发生什么。 – GSP 2013-02-26 19:40:38

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

+0

这是我最喜欢的答案,因为我不会这样想。 – GSP 2013-02-26 19:43:43

你的任务是很容易,你的问题标题暗示。您正在寻找从数组中随机挑选字符串,并且如果它与当前的字符串相同,请选择另一个字符串。

你已经知道如何访问这个字符串,也做

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;