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(' '); } };