-
Notifications
You must be signed in to change notification settings - Fork 1
/
scramble.ts
37 lines (30 loc) · 837 Bytes
/
scramble.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
If you mix up the order of letters in a word, many people can slitl raed and urenadnstd tehm.
Write a function that takes an input sentence, and mixes up the insides of words (anything longer than 3 letters).
Example:
> scramble(["A quick brown fox jumped over the lazy dog."])
> "A qciuk bwron fox jmepud oevr the lzay dog."
*/
function shuffleWord(word: string): string {
const sanitizedWord = word.replace(/\W/g, '');
if (sanitizedWord.length <= 3) {
return word;
}
const first = word.at(0);
const middle = word.slice(1, -1);
const last = word.at(-1);
return [
first,
middle
.split('')
.map((_, i) => middle[(i + 1) % middle.length])
.join(''),
last,
].join('');
}
export function scramble(sentence: string): string {
return sentence
.split(' ')
.map(word => shuffleWord(word))
.join(' ');
}