Javascript - Continuity of an array using arithmetic operations to find the index

Asked

Viewed 141 times

3

In Javascript, I, using in this case the for loop common iteration of array with the variable i, I am trying to create an arrangement with sub-arrangements that have data from other arrangements from an order relative to the position of i.

This code example describes situation:

var arranjoBase1 = ["a", "b", "c", "d", "e"]
var arranjoBase2 = ["f", "g", "h", "i", "j"]
var arranjoBase3 = [1, 2, 3, 4, 5]

var arranjoFinal = [ [], [], [], [], [] ]

for(i = 0; i < arranjoFinal.length; i++)
 { arranjoFinal[i].push(arranjoBase1[i], arranjoBase2[i], arranjoBase3[i]);
   if(i != 0) { arranjoFinal[i].push(arranjoBase3[i - 1]) }
    else { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 1]) };
   if(i != arranjoBase3.length - 1) { arranjoFinal[i].push(arranjoBase3[i + 1]) }
    else { arranjoFinal[i].push(arranjoBase3[0]) };
   if(i == 0) { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 2]) } 
    else if(i == 1) { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 1 ]) }
    else { arranjoFinal[i].push(arranjoBase3[i - 2]) };
   if(i == arranjoBase3.length - 1) { arranjoFinal[i].push(arranjoBase3[1]) }
    else if(i == arranjoBase3.length - 2) { arranjoFinal[i].push(arranjoBase3[0]) }
    else { arranjoFinal[i].push(arranjoBase3[i + 2]) }  };

//Resultado Final
  var arranjoFinal = (5) [Array(7), Array(7), Array(7), Array(7), Array(7)]

//Exemplo de Arranjo Final
  var  arranjoFinal[0] = (7) ["a", "f", 1, 5, 2, 4, 3]

This code already gives me the desired results.

The point is, I needed to use a lot of conditionals if to circumvent the problem of whether the result of one of the arithmetic operations to find the next element to be added to the Final arrangement is outside the index of the Base3 arrangement - in which case it would be returned Undefined in place of the desired element -, I feel that there must be a much more practical solution to achieve this result, which, for example, is also sustainable for arrangements greater than five elements.

So, is there really a more [practical/concise] way to achieve results like this? As, for example and preferably, a way of saying that if the result of an arithmetic operation to find the element index of a array exceed the content of that array this means that it is for her to continue the operation from her [beginning/end]?

  • Could you explain in more detail what the code should do or does today? if possible as if it were the explanation given to some developer who would develop this task for you. This way a possible solution will arise quite different from yours and may be better.

1 answer

0

All right, come on:

I believe your conditions are the least of your problems.

I didn’t try to improve the conditions because I was a little confused, I believe you need to think of a better logic.

But the issue is to improve javascript practices in this code.

A function that will greatly help your life is the map, it returns a new array from its input array (the original array is not modified, which means much in functional programming).

Your comparisons should be === instead of == to avoid type conversion since you know that the variables you are comparing are numbers.

Use const or Let, var is already in disuse. In my example I used only const because although I have to change the elements of an array I did not change its memory address.

Improve your identation: your conditionals are very difficult to understand, don’t put a semicolon at the end of an Else. I advise you to use the Eslint which provides rules for coding and ends up teaching you to program better.

As a last tip, learn functional programming and see why not changing an input variable is important for better maintenance and debugging of your code and learn Javascript in version 6.

I made a code using Arrow functions that is version 6 and already works on Chrome and firefox:

const arranjoBase1 = ["a", "b", "c", "d", "e"]
const arranjoBase2 = ["f", "g", "h", "i", "j"]
const arranjoBase3 = [1, 2, 3, 4, 5]

const arranjoBase = [undefined, undefined, undefined, undefined, undefined]

const arranjoMap = arranjoBase.map((el, i) => {
    const arranjoParte1 = [arranjoBase1[i], arranjoBase2[i], arranjoBase3[i]];
    const arranjoParte2 = [];

  if (i !== 0) {
    arranjoParte2.push(arranjoBase3[i - 1]);
  } else {
    arranjoParte2.push(arranjoBase3[arranjoBase3.length - 1]);
  }

  if(i !== arranjoBase3.length - 1) {
    arranjoParte2.push(arranjoBase3[i + 1]);
  }
  else {
    arranjoParte2.push(arranjoBase3[0]);
  }

  if (i === 0) {
    arranjoParte2.push(arranjoBase3[arranjoBase3.length - 2]);
  } else if (i === 1) {
    arranjoParte2.push(arranjoBase3[arranjoBase3.length - 1 ]);
  } else {
    arranjoParte2.push(arranjoBase3[i - 2]);
  }

  if (i === arranjoBase3.length - 1) {
    arranjoParte2.push(arranjoBase3[1]);
  }
  else if (i === arranjoBase3.length - 2) {
    arranjoParte2.push(arranjoBase3[0]);
  } else {
    arranjoParte2.push(arranjoBase3[i + 2]);
  }

  return arranjoParte1.concat(arranjoParte2);
});

As I will use the map, I only need an array with the number of elements you need. Since you only need the values of the other 3 arrays the value of the array doesn’t matter.

The Parte1 arrangement is always fixed and the Parte2 arrangement is treated in the conditions. At each interaction I return an array with the concatenation of the two.

In version 5 I use Concat to concatenate, in version 6 I can simply use this command:

[...arranjoParte1, ...arranjoParte2] 

Here is an example working, because after all speaking is easy right ;)

Browser other questions tagged

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