1

I am trying to get all combination of a number. For example, input "123" should return ["123", "231", "213", "312", "321", "132"].

Here is my function:

function swapDigits(input) {
    for (var i = 0; i++; i < input.length - 1) {
        var output = [];
        var inter = input.slice(i, i + 1);
        var left = (input.slice(0, i) + input.slice(i + 1, input)).split("");
        for (var j = 0; j++; j <= left.length) {
            var result = left.splice(j, 0, inter).join("");
            output.push(result);

        }
    }
    console.log(output);
    return output;
}

However this function returns undefined, could anyone tell me what's going wrong?

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
  • When asking for help, format your code readably. It's basic courtesy. (It also, in this case, makes at least one of the errors more obvious.) I've done it for you on this occasion. – T.J. Crowder Mar 10 '14 at 02:08
  • @crowder, take a closer look :) – alexmac Mar 10 '14 at 02:10
  • For starters, output is not declared in the right spot. Move it outside the for loop. – Ben van Gompel Mar 10 '14 at 02:12
  • 1
    It is 3! = 3 x 2 x 1 = 6 - if you refer to his code or his example :) he has all the possible in his example. In his code, well of course not :) – alexmac Mar 10 '14 at 02:13
  • @alexmac: Ah, the OP isn't allowing the same digit twice. – T.J. Crowder Mar 10 '14 at 02:13
  • Given that you already know how to use `console.log()`, did you try including some `console.log()` statements inside the outer and inner loops? That would make it obvious that the outer loop is executing zero times, which would in turn make it obvious why `output` doesn't get initialised to any particular value and thus remains `undefined`... – nnnnnn Mar 10 '14 at 02:17
  • No one notice his for loop is wrong? Folks are picking on the guy for his formatting etc and no one notices the for loop ? :) ' for (var i = 0; i++; i < input.length - 1)' which should be ' for (var i = 0; i < input.length - 1; i++)' – alexmac Mar 10 '14 at 02:27

2 Answers2

1

The errors with the for loop and scope have already been mentioned. Besides that, the splice method will change the string that it operates on. This means that the inner loop will never terminate because left keeps on growing, so j never reaches left.length.

If you are new to a language, I would suggest starting with an implementation that is close to the algorithm that you want to implement. Then, once you are comfortable with it, use more advanced language constructs.

See this fiddle for an example. This is the algorithm code:

function getPermutations(input)
{    
    if(input.length <= 1)
    {
        return [input];
    }
    var character = input[0];
    var returnArray = [];
    var subPermutes = getPermutations(input.slice(1));
    debugOutput('Returned array: ' + subPermutes);
    for(var subPermuteIndex = 0; subPermuteIndex < subPermutes.length; subPermuteIndex++ )
    {
       var subPermute = subPermutes[subPermuteIndex];
       for(var charIndex = 0; charIndex <= subPermute.length; charIndex++)
       {                         
           var pre = subPermute.slice( 0, charIndex );
           var post = subPermute.slice( charIndex );             
           returnArray.push(pre+character+post);           
           debugOutput(pre + '_' + character + '_' + post );
       }
    }
    return returnArray;
}

Basically, this will walk to the end of the string and work its way back constructing all permutations of sub-strings. It is easiest to see this from the debug output for 1234. Note that 'Returned array' refers to the array that was created by the permutations of the sub-string. Also note that the current character is placed in every position in that array. The current character is shown between _ such as the 1 in 432_1_.

Returned array: 4
_3_4
4_3_
Returned array: 34,43
_2_34
3_2_4
34_2_
_2_43
4_2_3
43_2_
Returned array: 234,324,342,243,423,432
_1_234
2_1_34
23_1_4
234_1_
_1_324
3_1_24
32_1_4
324_1_
_1_342
3_1_42
34_1_2
342_1_
_1_243
2_1_43
24_1_3
243_1_
_1_423
4_1_23
42_1_3
423_1_
_1_432
4_1_32
43_1_2
432_1_

This algorithm doesn't enforce uniqueness. So, if you have a string 22 then you will get two results - 22,22. Also, this algorithm uses recursion which I think is quite intuitive in this case, however there are pure iterative implementations if you look for them.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94
0

There are several errors in that code.

  1. You have the order of the parts of the for statement incorrect. The order is initialization, test, increment. So for (/* init */ ; /* test */ ; /* increment */)

  2. You're creating a new array for each iteration of your outer loop.

I'm making this a CW because I haven't checked for further errors than the above.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875