admin管理员组

文章数量:1355599

I am trying to shuffle words. I Want to shuffle First with its respective alphabets. Currently it is shuffling First with Second alphabets..

I Want to split words & shuffle "sFtir Seocdn".

String.prototype.shuffle = function () {
    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("");
}


alert("First Second".shuffle());

I tried splitting by below code, but then it only splits & shuffles words not letters.

var a = this.split(" "),
return a.join(" ");

Jsfiddle link : / Pls suggest what should I do.

I am trying to shuffle words. I Want to shuffle First with its respective alphabets. Currently it is shuffling First with Second alphabets..

I Want to split words & shuffle "sFtir Seocdn".

String.prototype.shuffle = function () {
    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("");
}


alert("First Second".shuffle());

I tried splitting by below code, but then it only splits & shuffles words not letters.

var a = this.split(" "),
return a.join(" ");

Jsfiddle link : http://jsfiddle/9L8rs/1/ Pls suggest what should I do.

Share Improve this question asked Jul 1, 2014 at 14:22 itsMeitsMe 6333 gold badges10 silver badges24 bronze badges 3
  • You need to call shuffle on the individual words... – Ian Commented Jul 1, 2014 at 14:25
  • 2 First split with a space, then loop through those results, and split with string.split(''). Then do your shuffle and join – bottens Commented Jul 1, 2014 at 14:25
  • 1 In addition to VisioN's answer, here's a solution keeping everything inside of shuffle (so the code using shuffle doesn't have to worry about words): jsfiddle/7hjg4 . But I guess it depends on how you're expecting to use this – Ian Commented Jul 1, 2014 at 14:30
Add a ment  | 

3 Answers 3

Reset to default 6

Just split the string into words and shuffle the letters in words separately:

"First Second".split(' ').map(function(w) { return w.shuffle(); }).join(' ');

Here .split and .map (polyfill may be applied for old browsers) methods can help.

DEMO: http://jsfiddle/9L8rs/2/

Try splitting the ining String first:

String.prototype.shuffle = function() {
    return this.split(" ").map(function(word, i) {
        var a = word.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("");
    }).join(" ");
}

alert("First Second".shuffle());

Updated JSFiddle

I just added a function shuffleWords that split on words and call your shuffle function on each of them. ex. a[i] = a[i].shuffle();

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

    for (var i = 0; i < n; i++) {
        a[i] = a[i].shuffle();
    }
    return a.join(" ");
}

See your updated jsfiddle

本文标签: javascriptSplit words amp shufflejumble lettersStack Overflow