خرید بک لینک

Vote count: 0

Probably a really basic question but I can't seem to figure out what is going on here. I am using the following function to find permutations. It works as I expect but when I look at what is being retued I am getting confused. When I just run the function getPermutations('abc'), it retus ['']. However, when I do console.log(getPermutations('abc')) I get the [''] and then I also get the answers retued in an array. Can someone explain this?

//====================================================
function getPermutations(str){
    //Enclosed data to be used by the inteal 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);

    retu permutations;
    //===========UNDER THE HOOD===========
    function permutate(letters){ //recursive: generates the permutations

            if(nextWord.length > 0){
                permutations.push(nextWord.join(''));                
            }


        for (var i=0; i < letters.length; i++){

            letters.push(letters.shift());  //rotate the characters // This removes first character and pushes it onto the back of the array 

            nextWord.push(letters[0]);    //use the first char in the array            

            permutate(letters.slice(1));  //Recurse: array-less-one-char  

            nextWord.pop();             //clear for nextWord (multiple pops)
        }
    }
    //--------------------------------
}//==============END of getPermutations(str)=============

//console.log("This is the answer ",getPermutations('abc'));

getPermutations('abc'); 

asked 52 secs ago

- - , .
.

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 22 تير 1395 ساعت: 3:15

صفحه بندی