用随机字母替换字符串中的任何3个字母

时间:2022-07-30 20:06:15

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

我想用字母变量中的随机字母替换字符串中的任意3个随机字母

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 am able to take 3 random letters out right now but can't figure out how to get 3 random letters from letters and put them in a random position.

我现在能够取出3个随机字母,但无法弄清楚如何从字母中获取3个随机字母并将它们放在随机位置。

Here is the demo of what I have right now.

这是我现在所拥有的演示。

http://jsfiddle.net/r1zd7rsw/

Any help would be appreciated!

任何帮助,将不胜感激!

4 个解决方案

#1


2  

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


2  

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

#3


1  

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 lacking was using the same random position selection method against your other string, and then using that position to feed a replacement character into your slice() call.

你所缺乏的只是对你的其他字符串使用相同的随机位置选择方法,然后使用该位置将替换字符输入到slice()调用中。

Changing things a bit more from your original, this would 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);

#4


1  

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


2  

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


2  

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

#3


1  

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 lacking was using the same random position selection method against your other string, and then using that position to feed a replacement character into your slice() call.

你所缺乏的只是对你的其他字符串使用相同的随机位置选择方法,然后使用该位置将替换字符输入到slice()调用中。

Changing things a bit more from your original, this would 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);

#4


1  

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/