Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function shuffle(arr) {
- const {acc} = arr.reduce(
- ({acc, current}) => {
- const index = Math.round(Math.random() * (current.length - 1))
- const item = current[index]
- current.splice(index, 1)
- return {acc: [...acc, item], current}
- },
- {acc: [], current: [...arr]}
- )
- return acc
- }
- const original = [1, 2, 3, 4, 5]
- console.log({original})
- console.log({shuffle: shuffle(original)})
Add Comment
Please, Sign In to add comment