JSFiddle - React, Tailwind, and code Playground
by stofke
HTML
<textarea name="" id="toJumble" cols="70" rows="10">According to a research at Cambridge University, it doesn't matter in what order the letters in a word are. The only important thing is that the first and last letter be in the right place. The rest can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself but the word as a whole.</textarea>
<br>
<textarea name="" id="result" cols="70" rows="10"></textarea>
<br>
<input type="button" id="test" value="test"></input>
JavaScript
Array.prototype.shuffle = function () {
for (var j, x, i = this.length; i; j = (Math.random() * i) | 0, x = this[--i], this[i] = this[j], this[j] = x);
return this;
};
String.prototype.jumble = function () {
var jumbled = this.split(/\W+/).map(function (strIn) {
var arrTmp = strIn.split('');
var arrFirst = arrTmp.slice(0, 1);
var arrLast = arrTmp.slice(-1);
var arrOut = [];
switch (arrTmp.length) {
case 1:
arrOut = arrFirst;
break;
case 2:
arrOut = arrOut.concat(arrFirst, arrLast);
break;
default:
var arrMiddle = arrTmp.slice(1, arrTmp.length - 1).shuffle();
arrOut = arrOut.concat(arrFirst, arrMiddle, arrLast);
}
var strOut = arrOut.join('');
return strOut;
}).join(' ');
return jumbled;
};
document.getElementById("test").onclick = function () {
var n = document.getElementById("toJumble").value;
var result = n.jumble();
document.getElementById("result").value = result;
};