Replace any 3 letters from the string with random letters

I want to replace any 3 random letters from a string with random letters from my variable letters

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split('');
for (var i = 0; i < 3; i++) {
    var pos = Math.round(Math.random() * arr.length - 1);
    arr.splice(Math.floor(Math.random() * arr.length), 1);
    str = arr.join('');
}
alert(str);

      

I can take 3 random letters right now, but I can't figure out how to get 3 random letters from letters

and put them in a random position.

Here is a demo of what I have now.

http://jsfiddle.net/r1zd7rsw/

Any help would be appreciated!

+3


source to share


4 answers


I tried a simpler approach:

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var strArray = str.split('');
var lettersArray = letters.split('');

for (var i = 0; i < 3; i++) {
    var pos1 = Math.round(Math.random() * (str.length - 1));
    var pos2 = Math.round(Math.random() * (letters.length - 1));
    strArray[pos1] = lettersArray[pos2];
}

alert(strArray.join(""));

      



DEMO: https://jsfiddle.net/erkaner/r1zd7rsw/7/

+2


source


You can do

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split(''),
    //a temp array to store already replaced locations
    temp = [],
    pos, char;
for (var i = 0; i < 3; i++) {
    //since you want 3 different chars to be replaced make sure the current position is not already replaced
    do {
        pos = Math.floor(Math.random() * arr.length);
    } while (temp.indexOf(pos) > -1);

    //find the new char, make sure it is not the same as the current character
    do {
        char = letters[Math.floor(Math.random() * letters.length)]
    } while (arr[pos] == char);

    //replace the character at position pos in the array arr, the character to be replaced is randomly selected from teh letters string
    arr[pos] = char;
    //store the current position in the temp array
    temp.push(pos);
}
str = arr.join('');
console.log(str);

      



Demo: Fiddle

+2


source


You were almost there!

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split('');
for (var i = 0; i < 3; i++) {
    var pos = Math.round(Math.random() * arr.length - 1);
    var replacementPos = Math.round(Math.random() * letters.length);
    arr.splice(Math.floor(Math.random() * arr.length), 1, letters[replacementPos]);
    str = arr.join('');
}
alert(str);

      

All you were missing was to use the same method of choosing a random position for your other string, and then use that position to feed the replacement character into your call slice()

.

By changing things a little more from your original, this will be easier to read:

var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var getPos = function(arr) {
    return Math.floor(Math.random() * arr.length);
}

var arr = str.split('');
for (var i = 0; i < 3; i++) {
    arr.splice(getPos(arr), 1, letters[getPos(letters)]);
}
str = arr.join('');
alert(str);

      

+1


source


function randString(num, myStr)
{
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for( var i=0; i < num; i++ ){
        randChar = possible.charAt(Math.floor(Math.random() * possible.length));
        repChar = myStr.charAt(Math.round(Math.random() * myStr.length));
        myStr = myStr.replace(repChar, randChar  );  
    }
    return myStr;
}

 alert(randString(3, "12345"))

      


DEMO http://jsfiddle.net/tuga/5h3jf3v0/2/

+1


source







All Articles