Last active
August 29, 2015 14:09
-
-
Save le717/4c2188ca814f56575a26 to your computer and use it in GitHub Desktop.
Randomly scrambles the given username.
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
/** | |
* Randomly scrambles the given username. | |
* @param {String} realName The username to be scrambled. | |
* @returns {String} The scrambled username. | |
*/ | |
function scrambleName(realName) { | |
"use strict"; | |
var newName = "", | |
usedChar = []; | |
function randomLetter(myString) { | |
return Math.floor(Math.random() * myString.length); | |
} | |
for (var i = 0; i < realName.length; i++) { | |
var letterIndex = randomLetter(realName); | |
// That letter has already been used | |
while (usedChar.indexOf(letterIndex) > -1) { | |
letterIndex = randomLetter(realName); | |
} | |
usedChar.push(letterIndex); | |
newName += realName.charAt(letterIndex); | |
} | |
return newName; | |
} | |
scrambleName("le717"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment