Javascript String shuffle()

Description

Javascript String shuffle()


String.prototype.shuffle = function() {
  var parts = this.split('');

  for (var i = 0, len = parts.length; i < len; i++) {
    var j = Math.floor( Math.random() * ( i + 1 ) );
    var temp = parts[i];
    parts[i] = parts[j];/*from ww  w.j ava  2 s .com*/
    parts[j] = temp;
  }

  return parts.join('');
};

Javascript String shuffle()

String.prototype.shuffle = function ()
{
    var characters = this.split(''),
        iterations = (characters.length-1),
        i, j, tmp;/*from  www .j  av  a  2s .  c o m*/

    for (i = iterations; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        tmp = characters[i];
        characters[i] = characters[j];
        characters[j] = tmp;
    }

    return characters.join('');
}

Javascript String shuffle()

var app = angular.module("ewlApp", []);

String.prototype.shuffle = function () {
    var a = this.split(""),
        n = a.length;//from   ww w  .  j a va  2s .  c  o  m

    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 pausecomp(millis)
 {
  var date = Date.now();
  var curDate = null;
  do { curDate = Date.now(); }
  while(curDate-date < millis);
}

Javascript String shuffle()

// Fisher-Yates Shuffle entry on Wikipedia
String.prototype.shuffle = function () {
    var a = this.split(""),
        n = a.length;//from w  w  w . j av a  2 s.c  o  m

    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 listAllPermutations(str){
  var list = [];

  for(var i = 0; i < Math.pow(str.length,str.length); i++) {
    var permutation = str.shuffle();
    if(list.includes(permutation) === false) {
      list.push(permutation);
    }
  }

  return list;

}
listAllPermutations('aab');

Javascript String shuffle()

String.prototype.shuffle = function () {
    var a = this.split(""),
        n = a.length;//  w w  w.  j  av a2 s  .co  m

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



PreviousNext

Related