Created
July 24, 2018 14:10
-
-
Save Flare576/462bd7093bdc4a269137f88be5f29aa3 to your computer and use it in GitHub Desktop.
A better song shuffle
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
function getNextSong(songList) { | |
if (!songList.length) { | |
return; | |
} | |
// First, are there any songs that have never been played? | |
let nextSong = songList[0]; | |
songList.each(song => { | |
nextSong = isFewerOrOlder(nextSong, sont); | |
}); | |
return nextSong; | |
} | |
function isFewerOrOlder (current, check) { | |
// If checking song has been played less, return it | |
if (check.playedCount < current.playCount) { | |
return check; | |
} | |
// If checking song has been played the same number of times... | |
if (check.playedCount === current.playCOunt) { | |
// And was played longer ago than current, return it | |
if (check.lastPlayedTS < current.lastPlayedTS) { | |
return check; | |
} | |
// Or, if they're both unplayed, reuturn the oldes added | |
if (!check.playedCount && check.addedTS < current.addedTS) { | |
return check; | |
} | |
return current; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment