Created
April 20, 2017 16:34
-
-
Save isterin/c9ad69fe9be75cc75540273096c4945b to your computer and use it in GitHub Desktop.
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 playlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
let N = process.argv[2] || 10; | |
function shuffle(a) { | |
const newA = a.slice(); // Copy array | |
for (let i = newA.length; i; i--) { | |
let j = Math.floor(Math.random() * i); | |
[newA[i - 1], newA[j]] = [newA[j], newA[i - 1]]; | |
} | |
return newA; | |
} | |
function playSound(sound) { | |
console.log(`Playing ${sound}`) | |
} | |
console.log(`Going to play ${N} times`) | |
let last; | |
for (let i = 0; i < N; i++) { | |
let shuffled = shuffle(playlist); | |
while (shuffled[0] == last) { | |
console.log(`DETECTED DUPLICATE, RESHUFFLNG ${shuffled}`); | |
shuffled = shuffle(playlist); | |
} | |
console.log(`SHUFFLED: ${shuffled}`) | |
for (let sound of shuffled) { | |
playSound(sound) | |
last = sound; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment