JavaScript
语言:
JaveScriptBabelCoffeeScript
确定
function shuffle(string) {
var array = string.split(""),
m = array.length,
t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array.join("");
}
function convert() {
var text = document.getElementById("input").value.trim()
console.log(text)
var words = text.split(" ");
words = words.map(function(word) {
if (word.length < 3) return word;
var first = word[0];
var middle;
var last = word[word.length - 1];
if (last == "," || last == "." || last == "?" || last == "!") {
last = word[word.length - 2] + last
middle = shuffle(word.substring(1, word.length - 2))
} else {
middle = shuffle(word.substring(1, word.length - 1));
}
return first + middle + last;
})
document.getElementById("output").innerHTML = words.join(" ");
}
document.getElementById("input").onkeyup = convert
document.getElementById("input").value = "Edit me! If you can read this, you have a strange mind too. Can you read this? Only some people can. I couldn't believe that I could actually understand what I was reading. The phenomenal power of the human mind, according to a research at Camebrigde 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 van be a total mess and you can still read it without a problem. This is because the human mind does not read every letter by itself, but the word as a whole. Amazing huh yeah and I always thought spelling was important!"
convert()