Created
October 8, 2012 11:39
-
-
Save soarez/3852067 to your computer and use it in GitHub Desktop.
Jumbler
This file contains hidden or 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
function randomizeLetters(string) { | |
var letters = string.split(''); | |
var result = ''; | |
while(letters.length) | |
result += letters.splice(Math.floor(Math.random() * letters.length), 1)[0]; | |
return result; | |
} | |
function jumbleWord(word) { | |
if (word.length < 3) | |
return word; | |
var first = word[0]; | |
var last = word[word.length-1]; | |
return first + randomizeLetters(word.slice(1, word.length - 1)) + last; | |
} | |
function jumble(string) { | |
var jumbled = []; | |
string.split(' ').forEach(function (w) { jumbled.push(jumbleWord(w)); }); | |
return jumbled.join(' '); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment