Created
September 16, 2019 20:23
-
-
Save 0x04/24edf4e135568bebbcb196560cffda3a to your computer and use it in GitHub Desktop.
Jumble letters - Except the first and last one, to keep it readable
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
// Jumble letters - Except the first and last one, to keep it readable | |
const jumbleWord = m => m[0] + [...m.substr(1, m.length - 2)].reverse().sort(() => Math.round(Math.random())).join('') + m[m.length - 1]; | |
const jumbleText = (t, i = 3) => t.replace(/\b\w(\w{2,})\w\b/gi, m => [...Array(i)].map(_ => jumbleWord(m)).find(e => e != m) || m); | |
// Test | |
let testText = "It doesn't matter in what order the letters in a word are, the only important\n" | |
+ "thing is that the first and last letter be at the right place. The rest can\n" | |
+ "be a total mess and you can still read it without problem. This is because\n" | |
+ "the human mind does not read every letter by itself, but the word as a whole." | |
console.log(jumbleText(testText, 5)); | |
console.log(jumbleText('If you can read that, your brain probably working fine.', 10)); | |
// Output | |
// > It deosn't mettar in waht oderr the leretts in a wrod are, the olny inatrompt | |
// tnhig is taht the fsirt and lsat lteter be at the rghit palce. The rset can | |
// be a toatl mses and you can slitl raed it wtouhit polebrm. Tihs is bacseue | |
// the hmuan mnid deos not raed eevry lteter by iletsf, but the wrod as a wolhe. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment