Javascript 无背对背的随机报价
我试图从数组中随机抽取一个引号。我需要显示一个初始报价,然后得到一个新的报价,没有相同的两个报价背靠背。这是我得到的Javascript 无背对背的随机报价,javascript,jquery,arrays,random,Javascript,Jquery,Arrays,Random,我试图从数组中随机抽取一个引号。我需要显示一个初始报价,然后得到一个新的报价,没有相同的两个报价背靠背。这是我得到的 $(document).ready(function() { var quoter = [{ quote: "I drink to make other people more interesting.", author: "Ernest Hemingway" }, { quote: "Alcohol may be man's worst ene
$(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
$(文档).ready(函数(){
变量报价器=[{
引用:“我喝酒是为了让别人更有趣。”,
作者:“欧内斯特·海明威”
}, {
引用:“酒精可能是人类最大的敌人,但圣经上说爱你的敌人。”,
作者:“弗兰克·西纳特拉”
}, {
引用:“现实是一种因缺乏酒精而产生的幻觉。”,
作者:“N.F.辛普森”
},{
引用:“当你一直在浪费时间的时候,时间永远不会被浪费。”,
作者:“凯瑟琳·赞多内拉”
},{
引用:“我为那些不喝酒的人感到难过。当他们早上醒来时,这和他们一整天的感觉一样好。”,
作者:“弗兰克·西纳特拉”
}];
var randomQuote=Math.floor(Math.random()*quoter.length);
//$(函数(){
//设置原始报价
$('quoteText').text(quoter[randomQuote].quote);
$('authorText').text(引号[randomQuote].author);
//});
$('#btnNew')。单击(函数(evt){
//阻止浏览器的默认操作
evt.preventDefault();
//获取要附加到报价的新随机数并设置限制
var sourceLength=quoter.length;
var randomNumber=Math.floor(Math.random()*sourceLength);
//设定新报价
//while(randomNumber您可以让一个简单的变量存储以前的值,然后检查随机数是否与上次相同
$(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;
});
});
您的方法可能会导致无限循环。不会发生,因为随机数生成器并不完美,但可以更轻松地完成:
或者线性向下(或者如果你愿意,可以构造一个幻觉),或者删除已经显示的每个引号。为什么不简单地保留从随机数生成器中获得的索引值并进行比较呢?如果(newNum==oldNum)reroll;var rand=myArray[Math.floor(Math.random()*myArray.length)];@mhodges我不知道该怎么做,谢谢@Vasim,但我不太明白,这是完全正确的。我不想写代码;)
$(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;
});
});