Last active
September 4, 2019 14:49
-
-
Save tamalchowdhury/6bc8f3bd4ec9e4c8b433b3bee4a895a5 to your computer and use it in GitHub Desktop.
This function takes a list items and randomizes them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {array} array which is an array of items to be shuffled | |
* A function which returns an array of random chests like: | |
* ['gold', 'half', 'silver'] | |
* or | |
* ['silver', 'gold', 'fifty'] | |
*/ | |
function shuffleArray(array) { | |
let output = []; | |
// While we still have items, keep rolling | |
while (array.length) { | |
// Pick one random item, fill the array, then remove that item | |
// Pick a number between 0-2 | |
let random = Math.floor(Math.random() * array.length); | |
output.push(array[random]); | |
// Remove the item from the array array | |
array.splice(random, 1); | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment