The Idea is: click a button to replace a headline with a unique string of an array. The problem with this is that I've used a string of the array before like this:
headlines = new Array("Good", "Bad", "Ugly", "Random Headline");
var randomNumberBefore = 4;
alert (headlines[randomNumberBefore]);
but I dont want to display the same headline again, thatswhy it is key check that the actual index randomNumberBefore
is not the same number like new index randomNumber
. The following function makes sense to me, but sometimes it returns the same number, that causes the headline is replaced by itself and the user noticed no change.
function randomNumberByRange (range, number) {
var r;
do {
var r = Math.floor(Math.random() * range);
} while (r == number);
return r;
}
$(document).on('click','.nextquote' , function() {
var randomNumber = randomNumberByRange( headlines.length, randomNumberBefore);
var nextHeadline = headlines[randomNumber];
$(".bannertext").text(nextHeadline);
console.log(nextHeadline);
});
Any Ideas to get unique headlines per click?
Here is my starting fiddle.
--
Here is the final fiddle.