我的目標:的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];
}
}
這很好用!謝謝!如果發現重複報價,我將如何獲得數學重複? – 2013-02-26 19:34:35
你的意思是如果選擇重複報價,你將如何再次運行該功能?只要將if部分改爲'if(rdmQuote == lastQuote){ quotes() }' – j08691 2013-02-26 19:43:27
當然!非常感謝! #weightoffmymind – 2013-02-26 19:47:25