I modified your code after getting the error message: "too much recursion" for string of about 6 characters or so.
I modified it below:
//====================================================
function getPermutations(str){
//Enclosed data to be used by the internal recursive function permutate():
var permutations = [], //generated permutations stored here
nextWord = [], //next word builds up in here
chars = [] //collection for each recursion level
;
//---------------------
//split words or numbers into an array of characters
if (typeof str === 'string') chars = str.split('');
else if (typeof str === 'number') {
str = str + ""; //convert number to string
chars = str.split('');//convert string into char array
}
//============TWO Declaratives========
permutate(chars);
return permutations;
//===========UNDER THE HOOD===========
function permutate(chars){ //recursive: generates the permutations
if(chars.length === 0)permutations.push(nextWord.join(''));
for (var i=0; i < chars.length; i++){
chars.push(chars.shift()); //rotate the characters
nextWord.push(chars[0]); //use the first char in the array
permutate(chars.slice(1)); //Recurse: array-less-one-char
nextWord.pop(); //clear for nextWord (multiple pops)
}
}
//--------------------------------
}//==============END of getPermutations(str)=============
Some of the names are long, but descriptive.
Features:
1.) No longer get the "too much recursion" message.
2.) Only one argument is passed to the function.
3.) Takes a string or a number argument.
4.) Variables are not global but enclosed in the outer function.
5.) The inner function does the recursion using closure variables
6.) Doesn't use splice, but uses slice only once.
Anything else you can add of value or take away would be appreciated.