Last active
March 6, 2023 14:23
-
-
Save frectonz/7157f84cd8ebf89b84efad685ccf3abf to your computer and use it in GitHub Desktop.
From cassido's March 6, 2023 Newsletter
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 scramble(sentences: string[]): string { | |
return sentences | |
.map((sentence) => | |
sentence | |
.split(" ") | |
.map((word) => { | |
if (word.length <= 3) { | |
return word; | |
} | |
const first = word[0]; | |
const middle = word.slice(1, word.length - 1); | |
const last = word[word.length - 1]; | |
return first + shuffle(middle) + last; | |
}) | |
.join(" ") | |
) | |
.join(" "); | |
} | |
function shuffle(word: string): string { | |
return word | |
.split("") | |
.sort(() => Math.random() - 0.5) | |
.join(""); | |
} | |
console.log(scramble(["A quick brown fox jumped over the lazy dog."])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment