乱码函数竞赛
请记住关于目标的离开信息,其中说明了如何:
根据 Cmabrigde Uinervtisy 的一位研究人员的说法,无论单词中的单词是什么,最重要的是第一个单词,而最后一个单词位于正确的位置。 rset 可以是一个 toatl mses,您可以直接读取它而无需 porbelm。这是因为,huamn mnid deos 不是由 istlef 提供的 ervey lteter,而是作为一个 wlohe 的 wrod。
无论如何,我正在尝试创建一个可以对整个页面执行此操作的函数。此函数有一些规则。
- 少于 4 个字符不受影响。
- 非字母数字字符不算作单词的一部分。
- 用连字符连接的单词实际上是两个单词
- 如果长度 >= 4,则单词必须出现乱码(不能像原始字符一样)
- 第一个和最后一个字符保持不变,只有中间的字符会出现乱码(感谢 Hersheezy)
- 文本应该始终是随机的并在每次运行时产生独特的乱码
- 纯JavaScript并在所有文本节点上迭代
- 最短最甜蜜的代码获胜。
无论如何,它似乎很容易实现,如何开始一场竞赛,看看谁可以编写最干净、最清晰的代码来完成这项任务。请随意借用而不需要我的代码认可(我肯定有)
如果我错过了任何内容,请在评论中添加它。不管怎样,我的工作非常黑客化,这是我展示我低于标准工作的作品
var i, j, words, textNodes, punct = /[^a-zA-Z0-9]/;
Array.prototype.shuffle = function() {
for (var i = 0; i < this.length; i++) {
var j = i;
while (j == i) {
j = Math.floor(Math.random() * this.length);
}
var tmp = this[i];
this[i] = this[j];
this[j] = tmp;
}
return this;
};
String.prototype.shuffle = function() {
return this.split('').shuffle().join('');
};
function transverse(element, array) {
if (!array) array = [];
if (element.nodeType === 3) {
array.push(element);
} else {
for (var i = 0; i < element.childNodes.length; i++) {
transverse(element.childNodes[i], array);
}
}
return array;
}
function garble(str) {
if (!str) return '';
str = str.trim();
if (/-/.test(str)) {
str = str.split('-');
for (var i = 0; i < str.length; i++) {
str[i] = garble(str[i]);
}
return str.join('-')
}
if (punct.test(str.charAt(0))) {
return str.charAt(0) + garble(str.slice(1));
}
if (punct.test(str.charAt(str.length - 1))) {
return garble(str.slice(0, -1)) + str.charAt(str.length - 1);
}
if (str.length < 4) return str;
if (str.length === 4) return str.charAt(0) + str.charAt(2) + str.charAt(1) + str.charAt(3)
return str.charAt(0) + str.substr(1, str.length - 2).shuffle() +
str.charAt(str.length - 1);
}
window.onload = function() {
textNodes = transverse(document.documentElement);
for (i = 0; i < textNodes.length; i++) {
words = textNodes[i].data.split(' ');
for (j = 0; j < words.length; j++) {
words[j] = garble(words[j]);
}
textNodes[i].data = words.join(' ');
}
};
Remember that away message on aim that said how:
Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat 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 wlohe.
Anyway I'm trying to make a function that would do that to an entire page. There are a few rules for this function.
- less then 4 characters leave alone.
- non-alphanumeric characters don't count as part of the word.
- hyphenated words are really two words
- words must get garbled if length >= 4 (can't be like the original)
- The first and last chars stay the same and only the middle chars get garbled (Thanks Hersheezy)
- the text should always be random and produce unique garbling on each run
- Pure javascript and iterates on all text nodes
- Shortest sweetest code wins.
Anyway it seems simple enough to implement, how's about starting a contest to see who could make the cleanest clearest code to accomplish this task. Feel free to borrow without recognition from my code (I def have)
If i missed anything add it in the comments. Anyway I worked on it very hackishly and here's me showing my less than par work
var i, j, words, textNodes, punct = /[^a-zA-Z0-9]/;
Array.prototype.shuffle = function() {
for (var i = 0; i < this.length; i++) {
var j = i;
while (j == i) {
j = Math.floor(Math.random() * this.length);
}
var tmp = this[i];
this[i] = this[j];
this[j] = tmp;
}
return this;
};
String.prototype.shuffle = function() {
return this.split('').shuffle().join('');
};
function transverse(element, array) {
if (!array) array = [];
if (element.nodeType === 3) {
array.push(element);
} else {
for (var i = 0; i < element.childNodes.length; i++) {
transverse(element.childNodes[i], array);
}
}
return array;
}
function garble(str) {
if (!str) return '';
str = str.trim();
if (/-/.test(str)) {
str = str.split('-');
for (var i = 0; i < str.length; i++) {
str[i] = garble(str[i]);
}
return str.join('-')
}
if (punct.test(str.charAt(0))) {
return str.charAt(0) + garble(str.slice(1));
}
if (punct.test(str.charAt(str.length - 1))) {
return garble(str.slice(0, -1)) + str.charAt(str.length - 1);
}
if (str.length < 4) return str;
if (str.length === 4) return str.charAt(0) + str.charAt(2) + str.charAt(1) + str.charAt(3)
return str.charAt(0) + str.substr(1, str.length - 2).shuffle() +
str.charAt(str.length - 1);
}
window.onload = function() {
textNodes = transverse(document.documentElement);
for (i = 0; i < textNodes.length; i++) {
words = textNodes[i].data.split(' ');
for (j = 0; j < words.length; j++) {
words[j] = garble(words[j]);
}
textNodes[i].data = words.join(' ');
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新(最新):不要认为它会变得更小.. DEMO
最新压缩(332):
代码:
更新甚至..更小..
甚至更小的版本
我不知道您使用的压缩器,但这必须至少小(编辑 108)字节。
压缩版本(365字节):
代码:
编辑
新规则演示
代码:
这应该尊重所有规则,并保留格式和标点符号。 DEMO
顺便说一句,这篇文章的不错选择,WWiWieikikb!
UPDATE( LATEST ): Don't think it can get any smaller.. DEMO
Latest compressed (332):
code:
UPDATE even.. smaller..
Even smaller version
I dont know the minifier your using, but this must be at least (EDIT 108) bytes smaller.
compressed version (365 bytes):
Code:
EDIT
NEW RULES DEMO
CODE:
This should respect all the rules, and keep format and punctuation. DEMO
btw nice choice of the article, WWiWieikikb!!
更新
所以我忍不住玩了一下这个东西,看看还有什么其他方法可以用尽可能少的代码来操作文档。我只想说它可以缩短以在非此即彼的场景中工作,但我喜欢制作带有选项供用户玩的东西。
话虽如此,以下是上述内容的一些变化以及优点/缺点:
正式提交(473 字节)
缩小(473 字节)1
未缩小版本: (479bytes) 1
或将代码包装在 onload 事件中。
1 重新放置 var 声明使其更短,请参阅 479bytes 与473 再见)
附加版本
基本 (演示)
优点
缺点
修身和修剪(演示)
功能齐全 (演示)
优点
上调用它 - 这可以处理它。
缺点
Updated
So I couldn't help but play around a bit with this thing and see what other ways I could manipulate the document with as little code as possible. Suffice it to say that it can be shortened to work in an either/or scenario, but I like to make things with options for the user to play with.
Having said that, here are some variations on the above and benefits/disappointments:
Official Submission (473bytes)
Minified (473bytes) 1
Un-minified version: (479bytes) 1
<script src="garble.js"></script>
just above</body>
or wrap the code in an onload event.1 re-placement of var declarations makes it shorter, see 479bytes vs 473 byes)
Additional Versions
Basic (demo)
Pros
Cons
Slim and Trim (demo)
Fully Featured (demo)
Pros
<body>
--this can handle it.Cons
这是我对该功能的看法。请参阅此处的演示。
完整版本(1187 字节):
缩小版本(348 字节):
Here's my take on the function. See the demo here.
Full version (1187 bytes):
Minified version (348 bytes):
注意:我通过 JSLint 运行了这个,这可能是一个坏主意。不管怎样,演示就在这里。
Note: I ran this through JSLint, which may have been a bad idea. Anyway, the demo is here.