How to make a generic loop in JS

0

I have programmed in other languages but now I am using JavaScript.

I have the following code:

function Mix2( ValoresJ ) {

    var ai = ValoresJ

    var l00 = ai[0]
    var l01 = ai[1]
    var l02 = ai[2]
    var l03 = ai[3]
    var l04 = ai[4]
    var l05 = ai[5]
    var l06 = ai[6]
    var l07 = ai[7]
    var l08 = ai[8]
    var l09 = ai[9]
    var l10 = ai[10]
    var l11 = ai[11]
    var l12 = ai[12]
    var l13 = ai[13]

    var i00 = 0
    var i01 = 0
    var i02 = 0
    var i03 = 0
    var i04 = 0
    var i05 = 0
    var i06 = 0
    var i07 = 0
    var i08 = 0
    var i09 = 0
    var i10 = 0
    var i11 = 0
    var i12 = 0
    var i13 = 0

    var tfinal = []
    var lfinal = []

      for (var i00=0;i00<l00.length;i00++){
        for (var i01=0;i01<l01.length;i01++){
          for (var i02=0;i02<l02.length;i02++){
            for (var i03=0;i03<l03.length;i03++){
              for (var i04=0;i04<l04.length;i04++){
                for (var i05=0;i05<l05.length;i05++){
                  for (var i06=0;i06<l06.length;i06++){    
                    for (var i07=0;i07<l07.length;i07++){
                      for (var i08=0;i08<l08.length;i08++){
                        for (var i09=0;i09<l09.length;i09++){
                          for (var i10=0;i10<l10.length;i10++){
                            for (var i11=0;i11<l11.length;i11++){
                              for (var i12=0;i12<l12.length;i12++){
                                for (var i13=0;i13<l13.length;i13++){    

                                  lfinal.push(l00[i00])
                                  lfinal.push(l01[i01])
                                  lfinal.push(l02[i02])
                                  lfinal.push(l03[i03])
                                  lfinal.push(l04[i04])
                                  lfinal.push(l05[i05])
                                  lfinal.push(l06[i06])
                                  lfinal.push(l07[i07])
                                  lfinal.push(l08[i08])
                                  lfinal.push(l09[i09])
                                  lfinal.push(l10[i10])
                                  lfinal.push(l11[i11])
                                  lfinal.push(l12[i12])
                                  lfinal.push(l13[i13])

                                  tfinal.push(lfinal)

                                  lfinal = []

                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

    var ss = SpreadsheetApp.getActiveSpreadsheet()

    ss.getRange("PRUEBA!B1:O36").setValues(tfinal)

}

Basically what I have is 14 cells where there is a series of characters and what I want is to obtain all the possible combinations of all the characters.

What I do is pass the contents of the cells to 14 arrays, each array will have a position for each character.

Then I make 14 loops and I get the Cartesian product.

It works well but I would like to know how to do it to be generic and so if in the future instead of being 14 it is 20 or 30, I do not have to go adding things.

    
asked by alberto Pablos Espada 31.05.2017 в 13:32
source

2 answers

0

You could do a recursive function. I know I found it around here and modified it, but I can not find the source.

If we have an array like this:

var ai = [
    ['a','b','c'],
    ['d','e','f'],
    ['g','h','i','j'],
    ...
];

we can generate the combinations with a function of this type:

function combinar(datos){
  if (datos.length == 1) {
    return datos[0];
  } else {
    var combinaciones = [];
    var restantes = combinar(datos.slice(1));
    for (var i = 0; i < restantes.length; i++) {
      for (var j = 0; j < datos[0].length; j++) {
        combinaciones.push(datos[0][j] + restantes[i]);
      }
    }
    return combinaciones;
  }

}

combinar(ai);
    
answered by 31.05.2017 в 14:19
0

I come from programming in ABAP and these things are usually done as they have proposed in the first response, you generate the names of the variables dynamically and you are accessing them with pointers or whatever.

But with JavaScript it is happening to me that whenever I do an algorithm to do something I get a terrible spawn, then you see that someone does the same but using two lines of code.

I will explore both options and comment here the results, thank you very much for responding so soon.

    
answered by 31.05.2017 в 15:59