Last active
February 4, 2020 05:48
-
-
Save sanishkr/ab4c012c000a8fd7d0f84bf043b86bf3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
function getRandomInt(max) { | |
return Math.floor(Math.random() * Math.floor(max)); | |
} | |
console.log("Initial Array", arr); | |
for(let i = (arr.length - 1); i > 0; i--){ | |
const randI = getRandomInt(i); | |
[arr[randI], arr[i]] = [arr[i], arr[randI]]; | |
} | |
console.log("Final Array", arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment