Skip to content

Instantly share code, notes, and snippets.

@frectonz
Last active March 6, 2023 14:23
Show Gist options
  • Save frectonz/7157f84cd8ebf89b84efad685ccf3abf to your computer and use it in GitHub Desktop.
Save frectonz/7157f84cd8ebf89b84efad685ccf3abf to your computer and use it in GitHub Desktop.
From cassido's March 6, 2023 Newsletter
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