Trying to shuffle multiple arrays in javascript but in the same way? -
this question has answer here:
i want randomly shuffle these 2 arrays in same function
var array1 = [1,2,3,4,5]; var array2 = [6,7,8,9,10];
so returns each array randomly shuffled such as
4,2,3,5,1 7,9,6,8,10
also on return want line break between two, please help?
added shuffle method array.prototype
easy access - returns modified array keeping original unchanged.
array.prototype.shuffle = function() { var rindex, temp, input = this.slice(0), cnt = this.length; while (cnt) { rindex = math.floor(math.random() * cnt); temp = input[cnt - 1]; input[cnt - 1] = input[rindex]; input[rindex] = temp; cnt--; } return input; } var array1 = [1, 2, 3, 4, 5]; var array2 = [6, 7, 8, 9, 10]; document.getelementbyid('shuffle-btn').onclick = function(){ document.getelementbyid('output').innerhtml = [array1.shuffle(), array2.shuffle()].join('\n'); }
<button id="shuffle-btn">shuffle</button> <pre id="output"></pre>
Comments
Post a Comment