I am trying to complete “No Repeats Please” challenge of #FreeCodeCamp.
I am not able to understand there is difference in output of result.push(array); and result.push(array.join()); Below is my code.
function permAlone(str) {
//Variables for main function
var result = [];
str = str.split("");
generateArray(str,3);
//function to swap the array members
function swap(array,pos1,pos2){
var temp;
temp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = temp;
}
//function to generate array Permutations
function generateArray(array,n){
if(n == 1){
result.push(array); //returns 2d array and with each element has "ABC"
//result.push(array.join('')); //return correct value
console.log(array);
}
else{
for(var i = 1;i<=n;i++){
generateArray(array,n-1);
if(n%2){ //If n is Odd
var j = 1;
}
else{
var j = i;
}
swap(array,j-1,n-1);//to counter zero indexing of JavaScript
}//end for
} //end else
} // end generateArray
console.log(result);
}
permAlone('ABC');