2013-02-26 48 views
0

我的目標:的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]; 
    } 
} 

回答

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

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

謝謝,但這似乎並不奏效。不管怎麼說,還是要謝謝你。當這個函數被調用時, – 2013-02-26 19:35:09

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

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,那麼這將是一個無限循環。但是,希望這是一個很好的起點。

+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

2

我會定義數組,使它只被初始化一次。然後我會選擇0到n-2之間的隨機條目m(n爲數組大小),並將第m條目與n-1條目交換並顯示此條目。因此新的選擇不能選擇當前顯示的條目。

+0

這是我最喜歡的答案,因爲我不會這樣想。 – GSP 2013-02-26 19:43:43

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; 
相關問題