If you ever need to randomly sort an array of items in JavaScript, here’s a simple function to help you do so:

var shuffleArray = function (array) { // Create a copy of the array so that we don't shuffle the original one var arrayCopy = array.slice(0); // Shuffle the array return arrayCopy.sort(function() { return Math.random() - 0.5 }) }

To use it, pass in any array.

var characters = ['Voldemort', 'Harry', 'Snape', 'Hagrid']; var shuffledCharacters = shuffleArray( characters ); // ex. return ["Harry", "Snape", "Hagrid", "Voldemort"]