First of all - here be spoilers for the “No repeats please” assignment. Part of my solution is visible below, so if you haven’t done it yet you might want to stop reading.
I’ve written the following code for creating an array with all possible permutations of a given string (it’s Heap’s permutation algorithm).
It works as expected, so if I evoke it with permAlone(‘abc’);
the output is:
[“acb”, “bac”, “cab”, “acb”, “bca”, “cba”]
The thing that I thoroughly don’t understand (and that made me search blindly for what was wrong with my code) is this:
What array.join() does is it takes all elements of an array and joins them into one string. So if I omit it from the code above, and write
permArr.push(strArr);
instead of
permArr.push(strArr.join(""));
I should get the same letters, in the same sequence - but each item in permArr should be a nested array of single letters instead of a string of letters. Right?
Apparently not, because without the .join
the output is:
[[“a”,“b”,“c”],[“a”,“b”,“c”],[“a”,“b”,“c”],[“a”,“b”,“c”],[“a”,“b”,“c”],[“a”,“b”,“c”]]
I don’t even know how to begin searching for an explanation of this. Could somebody point me in the right direction?