Nodejs String Shuffle shuffle()

Here you can find the source of shuffle()

Method Source Code

String.prototype.shuffle = function() {
    var arr = this.arrayise();
    for (var i = arr.length - 1; i > 0; i--) {
        var randIndex = Math.floor(Math.random() * (i + 1));
        var temp = arr[i];
        arr[i] = arr[randIndex];//w  w  w. ja  v  a2s .com
        arr[randIndex] = temp;
    }
    return arr.join('');
};

String.prototype.isSupersetAnagram = function(sub) {
    var subArr = sub.toUpperCase().arrayise();
    var thisArray = this.toUpperCase().arrayise();

    for (var i = 0; i < subArr.length; i++) {
        if (thisArray.indexOf(subArr[i]) < 0) {
            return false;
        } else {
            thisArray[thisArray.indexOf(subArr[i])] = '';
        }
    }
    return true;
};

Array.prototype.each = function(clos) {
    for (var i = 0; i < this.length; i++) {
        clos(this[i]);
    }
};
String.prototype.arrayise = function() {
    var arr = [];
    for (var i = 0; i < this.length; i++) {
        arr.push(this.charAt(i));
    }
    return arr;
};

Related

  1. shuffle()
    String.prototype.shuffle = function ()
        var characters = this.split(''),
            iterations = (characters.length-1),
            i, j, tmp;
        for (i = iterations; i > 0; i--) {
            j = Math.floor(Math.random() * (i + 1));
            tmp = characters[i];
            characters[i] = characters[j];
    ...
    
  2. shuffle()
    'use strict';
    String.prototype.shuffle = function () {
        var array = this.split('');
        for(var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var holder = array[i];
            array[i] = array[j];
            array[j] = holder;
        return array.join('');
    };
    
  3. shuffle()
    String.prototype.shuffle = function () {
        var array = this.split('');
        var tmp, current, top = array.length;
        if (top) while (--top) {
            current = Math.floor(Math.random() * (top + 1));
            tmp = array[current];
            array[current] = array[top];
            array[top] = tmp;
        return array.join('');
    };
    
  4. shuffle()
    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("");
    function createNewDocument() {
      var soup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
        documentId = "",
        length = 6;
      soup = soup.shuffle();
      while( documentId.length < length ) {
        var pos = Math.floor( Math.random() * soup.length ) + 1
        documentId += soup.charAt( pos );
      window.location.href = "/edit/" + documentId;
    };