Created
January 11, 2009 01:55
-
-
Save clindsey/45603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.jumble=function(){ | |
var words=this.split(" "); | |
var outputs=[]; | |
for(var i=0;i<words.length;i++){ | |
var word=words[i]; | |
var letters=word.split(""); | |
var output=""; | |
letters.pop();letters.shift(); | |
var k=letters.length; | |
if(k==0){outputs.push(word);continue;}; | |
while(--k){ | |
var l=Math.floor(Math.random()*(k+1)); | |
var temp_k=letters[k]; | |
var temp_l=letters[l]; | |
letters[k]=temp_l; | |
letters[l]=temp_k; | |
}; | |
word=word.charAt(0)+letters.join("")+word.charAt(word.length-1); | |
outputs.push(word); | |
}; | |
return outputs.join(" "); | |
}; | |
"some string to jumble".jumble(); // "smoe sitrng to jlmbue" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment