Javascript 生成字符串的所有组合的函数

Javascript 生成字符串的所有组合的函数,javascript,Javascript,我还不熟悉javascript,但仍在努力学习。 我找到了一个关于函数的解决方案,该函数应该生成字符串中所有字符的组合 我想弄明白: 循环内部发生了什么 循环是如何逐步执行的? 我不知道它是如何达到最终输出的 我花了很长时间想弄清楚,但我不知道里面发生了什么 循环。我不明白怎么会有“ab”,“ac”。。。在一起 在最终输出中,其中arrTemp推送结果[x]和字符。我看到结果数组最初是空的,然后与arrTemp连接 以下是我正在努力解决的代码: function combString(st

我还不熟悉javascript,但仍在努力学习。
我找到了一个关于函数的解决方案,该函数应该生成字符串中所有字符的组合

我想弄明白:

  • 循环内部发生了什么
  • 循环是如何逐步执行的?
    我不知道它是如何达到最终输出的
我花了很长时间想弄清楚,但我不知道里面发生了什么 循环。我不明白怎么会有“ab”,“ac”。。。在一起 在最终输出中,其中arrTemp推送结果[x]和字符。我看到结果数组最初是空的,然后与arrTemp连接

以下是我正在努力解决的代码:

 function combString(str){
     var lenStr = str.length;
     var result = [];
     var indexCurrent = 0;

     while(indexCurrent < lenStr){
         var char = str.charAt(indexCurrent);
         var x;
         var arrTemp = [char];

         for(x in result) {
             arrTemp.push(""+result[x]+char);
         }
         result = result.concat(arrTemp);

         indexCurrent++;
     }

     return result;
}

console.log(combString("abc"));

这里是注释代码,希望它能帮助您理解

function combString(str) {
    //String length
    var lenStr = str.length;
    //Initially empty, where the results will be stored
    var result = [];
    //Currently selected letter
    var indexCurrent = 0;

    //Looping from 0 to the length of the string
    //var char is selecting the character at this index. Ex: "a", then "b", then "c"
    while (indexCurrent < lenStr) {
        //Get the character at the index position. 
        var char = str.charAt(indexCurrent);

        var x;
        var arrTemp = [char];
        //For each previous result
        for (x in result) {
            //Add the current character to the index

            arrTemp.push("" + result[x] + char);

            /*
             * Ex: "abc"
             * First round: result is empty, so this code doesn't execute
             * Second round: result contains "a". Adds "ab" to the result array
             *  - Then. result array will contain "a","b" and "ab"
             * Third round: result contains "a","b","ab"
             *     For all of these results, add "c" to the resulting array
             *     Ex: "ac","bc", "abc"
             *  - Then add "c"
             */
        }
        result = result.concat(arrTemp);

        //Increment the current index to go to the next character 
        indexCurrent++;
    }

    return result;
}

console.log(combString("abc"));
函数组合字符串(str){
//字符串长度
var lenStr=str.length;
//最初为空,结果将存储在其中
var结果=[];
//当前选定的信函
var indexCurrent=0;
//从0循环到字符串长度
//var char正在选择此索引处的字符。例如:“a”,然后是“b”,然后是“c”
while(indexCurrent
假设输入为“ab”。下面是函数在没有循环的情况下的工作方式:

var str = "ab";

var lenStr = str.length;
var result = [];
var indexCurrent = 0;

var char, x, arrTemp;

//first while iteration begins

//indexCurrent === 0
//so char becomes "a"
char = str.charAt(indexCurrent);

//A temp array is created so it can be concatenated to the results array.
arrTemp = [char];
//arrTemp == ["a"]

//for-loop here, but since the result array is empty, it wont execute

//result becomes ["a"]
result = result.concat(arrTemp);

//indexCurrent becomes 1
indexCurrent++;

//second while iteration begins

//indexCurrent === 1
//so char becomes "b"
char = str.charAt(indexCurrent);

arrTemp = [char];
//arrTemp == ["b"]

//For-loop begins, x === 0  
//result[x] is the xth (in this case, first) value of the result-array
//the double quotes cast the result as string  
//in other words, it says:
//"store at the end of the array arrTemp, as string, the value from index x
//in the array result, plus the character stored in the variable char"
arrTemp.push(""+result[x]+char);

//arrTemp is now ["b", "ab"]

//result only has one item, so for-loop ends

//result becomes ["a", "b", "ab"]
result = result.concat(arrTemp);

//indexCurrent becomes 2
indexCurrent++;

//function returns result

请注意,
for in
循环不应用于对数组进行迭代(请参阅)。

好的,这很简单,首先我将为您注释代码,然后我将展示如何使用简单的字符串示例:

function combString(str){
 var lenStr = str.length;
 var result = [];
 var indexCurrent = 0;

 while(indexCurrent < lenStr){ // repeat until indexCurrent equals lenStr, the aim is to browse threw the string
     var char = str.charAt(indexCurrent); // select the char at indexCurrent
     var x;
     var arrTemp = [char];//put the selected char in an array

     for(x in result) {


    /*Here it's a little bit tricky, array are object, and actually
 the indexes of the array are properties which names are includes between 0 and
 2³²-2, but they can have other properties like any other object. that's 
the reason why you can use a for in loop here which will go threw the 
array and perform action on its properties with property name stored in the x variable (actually it is better to use a foreach loop) */


            arrTemp.push(""+result[x]+char); /* so here you concat the 
value of the current property of result (in the for in loop) with the char
 at indexCurrent and you add the concatenation result at the end of arrTemp */
     }
     result = result.concat(arrTemp); //here you concat result array and arrTemp and assign the concatenation result to result (I know there is a lot of result ahah)

     indexCurrent++; //and then you go to the next char in the string and you repeat 
 }
 // when the while loop ends you return result
 return result;
对于indexCurrent=1:

result = ['a'];
char = 'b';
arrayTemp (before for in loop) = ['b'];
arrayTemp (after for in loop) = ['b','ab']
result = ['a', 'b', 'ab'];
对于indexCurrent=2:

result = ['a', 'b', 'ab'];
char = 'c';
arrayTemp (before for in loop) = ['c'];
arrayTemp (after for in loop) = ['c','ac','bc','abc']
result = ['a', 'b', 'ab','c','ac','bc','abc'];

我希望这能帮助您

简单地使用for循环和while语句来获得不同的组合

function combu(s){
var buff = [];
var res = [];
for (i=0;i<s.length;i++){
    buff = [s[i]];
    var index=0;
    while(res[index]){
        buff.push(''+res[index]+s[i]);
        index++;
    }
    res = res.concat(buff);
}
return res;
}

combu('abc');
功能组合{
var buff=[];
var-res=[];

对于(i=0;i我们可以通过使用“切片”简单地实现它

function combinator (s) {
   list_of_strings = new Array();
   for(i=0;i<s.length;i++) {
       for(j=i+1;j<s.length+1;j++) {
           list_of_strings.push(s.slice(i, j));
       }
   }
   return list_of_strings;
}

document.write(combinator("dog"));
函数组合器{
列出所有字符串=新数组();

对于(i=0;用纸和铅笔玩电脑。从一个小的输入示例开始。这里几乎没有代码,培养理解你头脑中代码的能力很重要。你不应该也生成空字符串吗?这个函数并不能得到所有的组合-它省略了ca、cb、cba等。也许可以帮助你理解i你可以试着修改它来寻找那些组合吗?我知道它没有找到所有的组合,但我很难找到软管组合是如何进入阵列的,特别是像“ab”、“ac”、“bc”这样的组合,等等。我不确定这些循环是如何执行的,因为这是另一个循环中的一个循环,但我猜while执行2次,然后for循环一次,然后while再执行一次,然后for循环3次。我看到它将空的结果数组与arrttemp连接起来。因此arrttemp将包含在其数组[“a”]中,然后在下一次迭代中添加b,以便arrttemp=[“a”,“b”]但是它是如何将a和b像“ab”一样结合在一起的呢?在
for(x In result)
部分,当数组只包含“a”时,它会在将(b和ab)都添加到最终数组之前将“ab”附加到arrtep。谢谢你的回答这太棒了,谢谢!
function combu(s){
var buff = [];
var res = [];
for (i=0;i<s.length;i++){
    buff = [s[i]];
    var index=0;
    while(res[index]){
        buff.push(''+res[index]+s[i]);
        index++;
    }
    res = res.concat(buff);
}
return res;
}

combu('abc');
function combinator (s) {
   list_of_strings = new Array();
   for(i=0;i<s.length;i++) {
       for(j=i+1;j<s.length+1;j++) {
           list_of_strings.push(s.slice(i, j));
       }
   }
   return list_of_strings;
}

document.write(combinator("dog"));