Skip to content

Instantly share code, notes, and snippets.

@le717
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save le717/4c2188ca814f56575a26 to your computer and use it in GitHub Desktop.
Save le717/4c2188ca814f56575a26 to your computer and use it in GitHub Desktop.
Randomly scrambles the given username.
/**
* 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