Perform functions within an array, after a given index, and another for the others

Asked

Viewed 120 times

0

I will 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 take the first 5 indexes of each array and perform a function, then take the remaining indexes of that array and perform another function. If the remaining indices are greater than 5, I have to take the 5 and then the others and so on.

The checks, I have no problem, but I’m having a hard time finding a way to fit the functions in 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 besides these numerous ifs, I have not found a way, in each function within ifs, to use only the remaining indices for the function. I need to get to a certain index, pause, do the function, and go up to the next indices,.

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

Imagem ilustrativa

  • Put in the code you have so far, so we can help you better.

  • I put an example of what I tried

  • What kind of functions are those that have within the ifs ? If the execution is each 5 just use if (i !== 0 && i % 5 == 0). Or you can even do the for to the multiplicity of 5 below the size and do the remaining processing outside the for.

  • These functions are animated, using Tween. To try to explain, it’s a math game, each array is a set of blocks. Then there are two sets of block that are summed up. The answer is chosen by leaving a blank space on the floor for the blocks. The blocks then fit on the floor. First the sets of 5, then the others. It is hard to understand speaking like this, but I hope you have given a lightening rs

  • The problem is that if you only do it to the multiple of 5, the rest are out of function

  • How do I make sure the question doesn’t go as pending?

  • Let me get this straight. You want to perform a function on indices 5, 10, 15 [...] of an array, and on the other indices perform another function?

  • Not exactly indices 5, 10, 15... but indices up to 5, then indices up to 10, and while the remaining indices are not multiples of 5, type, only remain 3 indices, do another function

  • I edited the post with an image to try to lighten a little rs

  • Let me get this straight, first you have to perform the function with ALL sets of 5 that exist, only then do the function with sets smaller than 5?

  • That, the order is first the sets of 5 fitting into the ground, then the smallest

Show 6 more comments

1 answer

1


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

I tried to explain the code with comments, any doubt do not hesitate to ask.

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++;
}
  • Thank you very much! It was a little difficult to find a way out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.