【问题标题】:random quote without back to back没有背靠背的随机报价
【发布时间】:2016-05-10 06:27:09
【问题描述】:

我正在尝试从数组中提取随机引用。我需要显示一个初始报价,然后得到一个新报价,没有相同的两个报价背靠背。这就是我得到的。

$(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>

【问题讨论】:

  • 为什么不简单地保存从随机数生成器获得的索引值并进行比较? if (newNum === oldNum) 重新滚动;
  • var rand = myArray[Math.floor(Math.random() * myArray.length)];
  • @mhodges 我不知道该怎么做
  • 感谢@Vasim,但我不太了解

标签: javascript jquery arrays random


【解决方案1】:

你可以让一个简单的变量存储之前的值,然后检查随机数是否和上次一样。

$(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;    
    }); 
});

【讨论】:

  • 谢谢,这完全正确。我不想写出代码;)
【解决方案2】:

您的方法可能会导致无限循环。不会发生,因为随机数生成器并不完美,但可以更轻松地完成:

shuffel the array 并线性下降(或根据需要构造一个花哨的generator)或删除已显示的所有引用。

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    相关资源
    最近更新 更多