Skip to content

Instantly share code, notes, and snippets.

@bsturdivan
Last active March 9, 2024 00:12
Show Gist options
  • Save bsturdivan/91712c54c7a390d04529713b6abe1d55 to your computer and use it in GitHub Desktop.
Save bsturdivan/91712c54c7a390d04529713b6abe1d55 to your computer and use it in GitHub Desktop.
const BLOCK_SIZE = 4
const phrases = [
"Happy Birthday Lauren Conrad",
"Night Shade / Moon Beam",
"Escapement",
"Light / Leicht",
"Cap 3",
"Grapehene",
"Undercurrent / Watershed",
"Webtape",
"Re:Age",
"AirMake",
"Exit Music (For a Job)",
"Brunch at Foreign Cinema",
"SPQR 10pm",
"Wake",
"Eternal",
"Grown Up",
"Yearning",
]
const generator = () => Math.floor(Math.random() * phrases.length) - 1
const constructBlock = () => {
const textBlock = new Set()
while (textBlock.size !== BLOCK_SIZE) {
const index = generator()
const phrase = phrases[index]
textBlock.add(phrase)
}
return [...textBlock].sort(
(a, b) => a.length - b.length
)
}
const swap = (index, block ) => {
const newPhrase = phrases[generator()]
if (index === 0) {
const [first, ...remaining] = block
const remainingLengths = remaining.map((item) => item.length)
if (newPhrase.length < Math.min(remainingLengths)) {
return newPhrase
} else {
swap(index, block)
}
} else if (index === 1) {
const [first, second, ...remaining] = block
const remainingLengths = remaining.map((item) => item.length)
if (
newPhrase.split("").length > first &&
newPhrase.split("").length > Math.min(remainingLengths) &&
newPhrase.split("").length < Math.max(remainingLengths)
) {
return newPhrase
} else {
swap(index, block)
}
} else {
const [first, second] = block
if (
newPhrase.split("").length > Math.min(second) ||
newPhrase.split("").length > Math.min(first)
) {
return newPhrase
} else {
swap(index, block)
}
}
}
const printBlock = () => {
const block = constructBlock()
const remaininItems = [block[1], block[3]].sort((a, b) => 0.5 - Math.random())
setInterval(() => {
index = Math.floor(Math.random() * 4) - 1
swap(index, block)
}, 5000)
console.log([block[0], block[2], ...remaininItems])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment