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.
Any help would be appreciated!
source to share
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(""));
source to share
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
source to share
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);
source to share
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"))
source to share