含有js的英文单词_JS脚本实现英文单词内部字母乱序组合

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()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值