0

I want to remove 3 RANDOM letters from a string.

I can use something like substr() or slice() function but it won't let me take the random letters out.

Here is the demo of what I have right now.

http://jsfiddle.net/euuhyfr4/

Any help would be appreciated!

user4756836
  • 1,277
  • 4
  • 20
  • 47
  • Remove a random 3 letter sequence from the string? such as abc'123' or a'bc1'23 where the '' denotes whats removed? – gsp8181 Apr 26 '15 at 15:38

6 Answers6

3
var str = "hello world";
for(var i = 0; i < 3; i++) {
    str = removeRandomLetter(str);
}
alert(str);

function removeRandomLetter(str) {
    var pos = Math.floor(Math.random()*str.length);
    return str.substring(0, pos)+str.substring(pos+1);
}

If you want to replace 3 random charc with other random chars, you can use 3 times this function:

function substitute(str) { 
    var pos = Math.floor(Math.random()*str.length); 
    return str.substring(0, pos) + getRandomLetter() + str.substring(pos+1); 
} 
function getRandomLetter() { 
    var  letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 
    var pos = Math.floor(Math.random()*letters.length); 
    return letters.charAt(pos); 
}
Pavel Rudko
  • 248
  • 2
  • 8
  • Would it be possible to make it so that instead of removing the 3 letters... replace any 3 letters of the word with 3 random letters? – user4756836 Apr 26 '15 at 16:48
  • Yes, of course. `function subsctitute(str) { var pos = Math.floor(Math.random()*str.length); return str.substring(0, pos)+getRandomLetter()+str.substring(pos+1); } function getRandomLetter() { var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var pos = Math.floor(Math.random()*letters.length); return letters.charAt(pos); }` – Pavel Rudko Apr 26 '15 at 16:59
1

You can split the string to an array, splice random items, and join back to a string:

var arr = str.split('');
for(var i=0; i<3; ++i)
    arr.splice(Math.floor(Math.random() * arr.length), 1);
str = arr.join('');
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Would it be possible to make it so that instead of removing the 3 letters... replace any 3 letters of the word with 3 random letters? – user4756836 Apr 26 '15 at 16:49
  • @user4756836 Yes: `arr[Math.floor(Math.random() * arr.length)] = randomLetter()`. The definition of `randomLetter` will vary depending on what characters you want (a-z, A-Z, digits, latin1, ...). – Oriol Apr 26 '15 at 17:12
1
var str = "cat123",
    amountLetters = 3,
    randomString = "";

for(var i=0; i < amountLetters; i++) {
  randomString += str.substr(Math.floor(Math.random()*str.length), 1);
}
alert(randomString);

fiddle: http://jsfiddle.net/euuhyfr4/7/

jvecsei
  • 1,936
  • 2
  • 17
  • 24
  • Would it be possible to make it so that instead of removing the 3 letters... replace any 3 letters of the word with 3 random letters? – user4756836 Apr 26 '15 at 16:49
1

This answer states that

It is faster to slice the string twice [...] than using a split followed by a join [...]

Therefore, while Oriol's answer works perfectly fine, I believe a faster implementation would be:

function removeRandom(str, amount)
{
    for(var i = 0; i < amount; i++)
    {
        var max = str.length - 1;
        var pos = Math.round(Math.random() * max);
        str = str.slice(0, pos) + str.slice(pos + 1);
    }
    return str;
}

See also this fiddle.

Community
  • 1
  • 1
Siguza
  • 21,155
  • 6
  • 52
  • 89
  • Would it be possible to make it so that instead of removing the 3 letters... replace any 3 letters of the word with 3 random letters? – user4756836 Apr 26 '15 at 16:48
1

you can shuffle characters in your string then remove first 3 characters

var str = 'congratulations';

String.prototype.removeItems = function (num) {
    var a = this.split(""),
        n = a.length;

    for(var i = n - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
    return a.join("").substring(num);
}

alert(str.removeItems(3));
Egy Success
  • 112
  • 1
  • 1
  • 8
  • Would it be possible to make it so that instead of removing the 3 letters... replace any 3 letters of the word with 3 random letters? – user4756836 Apr 26 '15 at 16:48
-1

You can use split method without any args. This would return all chars as a array.

Then you can use any randomiser function as described in Generating random whole numbers in JavaScript in a specific range? , then use that position to get the character at that position.

Have a look @ my implementation here

var str = "cat123";
var strArray = str.split("");

function getRandomizer(bottom, top) {
        return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
    }
alert("Total length " + strArray.length);
var nrand = getRandomizer(1, strArray.length);
alert("Randon number between range 1 - length of string " + nrand);

alert("Character @ random position " + strArray[nrand]);

Code @ here https://jsfiddle.net/1ryjedq6/

Community
  • 1
  • 1
kspviswa
  • 637
  • 3
  • 13
  • Would it be possible to make it so that instead of removing the 3 letters... replace any 3 letters of the word with 3 random letters? – user4756836 Apr 26 '15 at 16:49
  • @user4756836 can you be more specific? you want to replace random chars with random positions? – kspviswa Apr 26 '15 at 16:56
  • I want to replace 3 random chosen letters from the word with ANY random letter from a-z. For example, if the word is Hello Kitty... it may change to Hezls Kilty – user4756836 Apr 26 '15 at 16:59
  • Yeah it very simple. Define a array with all English alphabets. From the random number generator generate 2 numbers. Num1 will be the position of the string, where the char needs to be replaced and Num2 will be index to the array of english alphabets. (or) re-use the same number again to pick up a new char from the alphabets array. – kspviswa Apr 26 '15 at 17:07