Created
March 31, 2018 03:44
-
-
Save imshubhamsingh/44422d532f3111ba253c55dfd462dde8 to your computer and use it in GitHub Desktop.
Shuffle Song List in Javascript in O(n)
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
const shuffleSongs = (songList) => { | |
let index = Date.now() % songList.length | |
let list = [] | |
for (var i = 0; i < songList.length/2; i++) { | |
if (index + i < songList.length) { | |
list.push(songList[index + i]) | |
} else { | |
break; | |
} | |
if (i === 0) continue; | |
if (index - i >= 0) { | |
list.push(songList[index - i]) | |
} else { | |
break; | |
} | |
} | |
for (var j = index + i + 1; j < songList.length; j++) { | |
list.push(songList[j]) | |
} | |
for (var j = index - i; j >= 0; j--) { | |
list.push(songList[j]) | |
} | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment