Perform functions inside an array, after a given index, and another for the other

0

I'll try to be brief in the description. I have, for example, two arrays. One of them has 6 indexes and the other 7.

I need to get the first 5 indexes of each array and perform a function, then I have to get the remaining indexes from that array and perform another function. If the remaining remaining indexes are greater than 5, I have to get the 5 and then the others, and so on.

The checks, I have no problem, but I'm having trouble finding a way to fit the functions into each of the indexes

Edit: I tried something like this:

for (let i = 0; i < array.length; i++){
   if (i === 5) {
     // executa função para mod de 5
   } else if (i > 5) {
     if (i % 5 === 0) {
      // executa função para maiores do 5, que formam outro conjunto de 5
    } else {
      // executa função para maiores do 5, mas que não formam um novo conjunto de 5
    } 
 }

The problem is that in addition to these numerous ifs, I have not found a way for, at every function within the ifs, to use only the remaining indexes for the function. I need to get into a certain index, pause, do the function, and go to the next indexes, do the checking, and perform other functions.

To illustrate better, in the image I have two arrays, one on the left side with 6 indexes, containing the blocks and another one on the right side, with 7 indexes, containing the blocks. The function must first call sets of 5 blocks to go to ground, then sets that do not have 5 remaining indices in the array.

    
asked by anonymous 09.08.2018 / 22:52

1 answer

1

Here below is the demonstration using 1 array, to use the second, just copy and change some variables.

I tried to explain the code with comments, any questions do not exist in asking.

array1 = [];
array2 = [];
posFinalConjunto5Array1 = 0;
for (let i = 0; i<20; i++){ //ADICIONANDO ELEMENTOS NOS ARRAYS PARA DEMONSTRAÇÃO, IGNORAR NO SEU PROGRAMA
    array1.push(i);
    array2.push(i);
}
for(let contConjuntos5 = 1;contConjuntos5 <= (array1.length/5); contConjuntos5++){ //PERCORRE A QUANTIDADE DE CONJUNTOS DE 5 NO ARRAY 1
    i=(contConjuntos5*5)-5;
    while(i<5*contConjuntos5){
        // PERCORRE TODOS OS VALORES DE CADA CONJUNTO DE 5
        // executa função para mod de 5
        i++;
        if((array1.length-i)<5){
            posFinalConjunto5Array1 = i;
        }
    }
}
while (posFinalConjunto5Array1<array1.length){
    // PERCORRE VALORES RESTANTES
    // executa função para maiores do 5, mas que não formam um novo conjunto de 5
    posFinalConjunto5Array1++;
}
    
10.08.2018 / 16:57