JSFiddle - React, Tailwind, and code Playground
HTML
Aoccdrnig to a rscheearch at <b>Cmabrigde Uinervtisy</b>, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the <i>frist</i> and <i>lsat</i> ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a <u>wlohe</u>.
JavaScript
function shuffle(letters) {
var i = letters.length - 2;
while (i > 1) {
var pos = Math.floor(Math.random() * i) + 1;
var tmp = letters[i];
letters[i] = letters[pos];
letters[pos] = tmp;
i--;
}
}
function scramble(word) {
if (word.slice(1, -2) == word.slice(2, -1)) {
return word;
}
var letters = word.split('');
var result = word;
while (result == word) {
shuffle(letters);
result = letters.join('');
}
return result;
}
function process(node) {
var data = node.data;
if (/[a-z]{4}/i.test(data)) {
node.data = data.replace(/[a-z]{4,}/gi, scramble);
}
}
function traverse(element) {
var node = element.firstChild;
while (node) {
if (node.nodeType == Node.ELEMENT_NODE) {
traverse(node);
} else if (node.nodeType == Node.TEXT_NODE) {
process(node);
}
node = node.nextSibling;
}
}
function garble() {
traverse(document.body);
}
window.onload = garble;