2016-02-01 85 views
1

我試圖從數組中拉出一個隨機引用。我需要顯示一個最初的報價,然後得到一個新的報價,兩個報價沒有相同的背靠背。這是我得到的。沒有背對背的隨機報價

$(document).ready(function() { 

    var quoter = [{ 
    quote: "I drink to make other people more interesting.", 
    author: "Ernest Hemingway" 
}, { 
    quote: "Alcohol may be man's worst enemy, but the bible says love your enemy.", 
    author: "Frank Sinatra" 
}, { 
    quote: "Reality is an illusion created by a lack of alcohol.", 
    author: "N.F. Simpson" 
},{ 
    quote: "Time is never wasted when you’re wasted all the time.", 
    author: "Catherine Zandonella" 
},{ 
    quote: "I feel bad for people who don’t drink. When they wake up in the morning, that’s as good as they’re going to feel all day.", 
    author: "Frank Sinatra"  
}]; 

    var randomQuote = Math.floor(Math.random() * quoter.length); 
//$(function() { 
    //Set Original Quote 
    $('#quoteText').text(quoter[randomQuote].quote); 
    $('#authorText').text(quoter[randomQuote].author);  
//});  

    $('#btnNew').click(function(evt) { 
    //prevent browser's default action 
    evt.preventDefault(); 
    //getting a new random number to attach to a quote and setting a limit 
    var sourceLength = quoter.length; 
    var randomNumber = Math.floor(Math.random() * sourceLength); 


    //set a new quote 
    //while (randomNumber <= sourceLength) { 
     while (randomNumber === randomNumber){ 
     var newQuoteText = quoter[randomNumber].quote; 
     var newQuoteGenius = quoter[randomNumber].author; 

     var timeAnimation = 500; 
     var quoteContainer = $('#quoteContainer'); 
     //fade out animation with callback 
     quoteContainer.fadeOut(timeAnimation, function() { 

     //set text values 
     $('#quoteText').text(newQuoteText); 
     $('#authorText').text(newQuoteGenius); 

     //console.log(quoteText,authorText); 

     //fadein animation. 
     quoteContainer.fadeIn(timeAnimation); 
     }); 

     break; 

    }; //end while loop 

    }); //end btnNew function 

}); //end document ready 

我需要通過使用while循環來做到這一點。我無法弄清楚如何存儲隨機引用(數組值),然後將它與新隨機引用進行比較,以獲取不同的隨機值,如果相同的話。

的HTML是在這裏:

<div id="quoteContainer"> 
    <p><span id="quoteText"></span><Br/></p> 
    &mdash;<span id="authorText"> </span> 
</div> 
+0

爲什麼不簡單地堅持從你的隨機數發生器得到的索引值並進行比較? if(newNum === oldNum)reroll; – mhodges

+0

var rand = myArray [Math.floor(Math.random()* myArray.length)]; –

+0

@mhodges我不知道如何做到這一點 – mcadamsjustin

回答

2

你可以有一個簡單的變量商店以前的值,然後檢查是否隨機數是一樣的最後一次。

$(document).ready(function(){ 
    //....your current above code 

    var lastQuote = randomQuote; 

    $(button).click(function(){ 
     var thisQuote = Math.floor(Math.random() * sourceLength); 

     //This will only be entered if the two are equal 
     //The while loop ensures that if you get a new random number 
     //it won't be the same 
     while(thisQuote == lastQuote){ 
      thisQuote = Math.floor(Math.random() * sourceLength); 
     } 

     //If you make it here a unique number has been found 
     lastQuote = thisQuote;  
    }); 
}); 
+0

謝謝,這是完全正確的。我不想寫出代碼;) – mhodges

+0

這工作,謝謝 – mcadamsjustin

1

您的方法可能會導致無限循環。 (如果你想或構建看中generator

無論shuffel the array和線性下降或只是刪除已顯示的每個報價:不會發生,因爲隨機數發生器是不完美的,但它可以做到更輕鬆已經。