Skip to content

Instantly share code, notes, and snippets.

@nicolehe
Created February 8, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolehe/4527e629c1b90d5f14e30fcff7364be4 to your computer and use it in GitHub Desktop.
Save nicolehe/4527e629c1b90d5f14e30fcff7364be4 to your computer and use it in GitHub Desktop.
// shuffle
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// get all image urls
var imageUrls = [];
$('img').each(function() {
image = $(this)[0].src;
imageUrls.push(image);
});
// replace each image url with a random one from the page
$('img').each(function() {
randomImage = imageUrls[Math.floor(Math.random() * imageUrls.length)];
$(this)[0].src = randomImage;
});
// shuffle text
$('p, h1, h2, h3, li').each(function() {
var text = $(this).text().trim();
if (text !== "") {
var splitted = text.split(" ");
var shuffled = shuffle(splitted).join(" ");
$(this).text(shuffled);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment