Javascript String swap(a, b)

Description

Javascript String swap(a, b)


String.prototype.swap = function(a, b) {
    var array = this.split('');
    var tmp = array[a];
    array[a] = array[b];//from w  w w  .ja  v a  2s. c  o  m
    array[b] = tmp;
    return array.join('');
}

function permutation(str, start) {
    if (start == str.length - 1) {
        console.log(str);
    } else {
        for (var i = start; i < str.length; i++) {
          permutation(str.swap(start, i), start+1);
       }
   }
}

permutation('abc', 0);



PreviousNext

Related