Files
JavaScript/Sorts/FisherYatesShuffle.js
Omkarnath Parida 0ada757576 Update FisherYatesShuffle.js
Splited initialized 'let' declarations into multiple statements
2021-10-03 20:08:02 +05:30

25 lines
552 B
JavaScript

export const shuffle = (array) => {
let maxLength = array.length
let temp
let idx
// While there remain elements to shuffle...
while (maxLength) {
// Pick a remaining element...
idx = Math.floor(Math.random() * maxLength--)
// And swap it with the current element
temp = array[maxLength]
array[maxLength] = array[idx]
array[idx] = temp
}
return array
}
const array = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
console.log('array', array)
const mixedArray = shuffle(array)
console.log('mixedArray', mixedArray)