user4756836
user4756836

Reputation: 1337

Take random letters out from a string

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!

Upvotes: 0

Views: 4434

Answers (6)

Pavel Rudko
Pavel Rudko

Reputation: 268

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); 
}

Upvotes: 3

Egy Success
Egy Success

Reputation: 112

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));

Upvotes: 1

Siguza
Siguza

Reputation: 23880

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.

Upvotes: 1

kspviswa
kspviswa

Reputation: 657

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/

Upvotes: -1

jvecsei
jvecsei

Reputation: 1993

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/

Upvotes: 1

Oriol
Oriol

Reputation: 288590

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('');

Upvotes: 1

Related Questions