JSFiddle - React, Tailwind, and code Playground

by aanita0309

JavaScript

// Given array of char print all permutations. Use recurrsion
// Recurrsion is cutting big problem into small similar problem



function printPerm(charArray, startPos){
//alert("hi");
	// array contains only 1 element
  if( charArray.length == 1){
       document.writeln(charArray.toString());
  }
  else{
  		for(var j=startPos; j<charArray.length;j++){
      	var newPerm = Swap(charArray,startPos,j)
       document.writeln(newPerm);
			
      }
  		printPerm(charArray,startPos+1);
  }

}

function Swap(inputArray,swapCharPos, swapWithCharPos){

var temp = intputArray[swapCharPos];
inputArray[swapCharPos] = inputArray[swapWithCharPos];
inputArray[swapWithCharPos] = temp;

return inputArray[swapCharPos] .toString();
}



printPerm(['b','d','e','f'],0);