Skip to content

Instantly share code, notes, and snippets.

@1Marc
Created August 12, 2015 13:04
Show Gist options
  • Save 1Marc/3d1b99938d679ba994c1 to your computer and use it in GitHub Desktop.
Save 1Marc/3d1b99938d679ba994c1 to your computer and use it in GitHub Desktop.
random DOM nodes
var comments = document.querySelectorAll('#comment-list > li');
var commentsList = [];
var array = new Uint32Array(comments.length);
var randomIds = window.crypto.getRandomValues(array);
Array.prototype.forEach.call(comments, function(el, i){
if (el.id) commentsList.push({id: el.id, rand: randomIds[i]});
});
commentsList.sort(function(a, b) {
return a.rand - b.rand;
});
getWinnerComment = function(id) {
return document.querySelectorAll('#' + id + ' .comment-content p')[0].textContent;
}
console.log(commentsList[0].id, getWinnerComment(commentsList[0].id));
console.log(commentsList[1].id, getWinnerComment(commentsList[1].id));
console.log(commentsList[2].id, getWinnerComment(commentsList[2].id));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment