I'm developing a recommendation widget that shows 4 random articles at the bottom of a blog post.
To do this, I need to append a random number between 0 through 5 to the end of the URL. This number can't repeat. Also, it only needs to perform the function until 4 articles are shown.
At the moment, the random numbers are generated, but they repeat. How do I write the jQuery code below so these numbers don't repeat?
HTML
<div class="recommended""></div>
jQuery
$(window).on("load", function() {
var digits = [];
for(var i = 0; i < 5; i++)
digits.push(i);
function timer() {
var digit = digits.splice( Math.floor( Math.random() * digits.length ), 1 );
var blogUrl = 'www.website.com/';
var together = blogUrl + digit;
$.get(together, function(data) {
var recommendedArticles = $(data).find(".blog-title a");
$(".recommended").append(recommendedArticles);
});
}
var articleCount = $(".recommended a").length;
for (; articleCount < 4; articleCount++) {
timer();
}
});