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

3

In JavaScript, I, using the common for loop iteration of array with variable i , I'm trying to create an array with sub-arrangements having data from other arrangements from an order relative to the position of i .

This code example describes the 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 results you want.

The point is that since I needed to use many if conditionals to get around the problem of whether the result of one of the arithmetic operations to find the next element to be added in the End array is out of the array indexBase3 - in which case it would be returned undefined instead of the desired element - I feel that there must be a much more practical solution to this result, which, for example, is also sustainable for larger arrays than five elements.

So, would there really be a more [practical / concise] way of achieving results like these? As, for example, and preferably, a way of saying that if the result of an arithmetic operation to find the index of the element of an array exceeds the index of that array this means which is for it to continue the operation from its [beginning / end]?

    
asked by anonymous 08.12.2017 / 04:15

1 answer

0

Ok, let's go:

I believe your conditions are the least of your problems.

I did not try to improve conditions because I was a bit confused, I think you need to think about a better logic.

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

One function that will greatly help your life is map , it returns a new array from its input array (the original array is not modified, which means a lot 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 deprecated. In my example I used const just because despite having to change the elements of an array I did not change the memory address of it.

Improve your indentation: your conditionals are hard to understand, do not put a semicolon at the end of an else. I advise you to use eslint that provides rules for coding and teaches you how to program better.

As a final tip, learn functional programming and see why not switching 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 in 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'm going to use the map, I just need an array with the number of elements you need. Since you only need the values of the other 3 arrays, the array value does not matter.

The arrangementPart1 is always fixed and the arrangementPart2 is treated in the condition. 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 a working example, because after all speaking is easy, right?)

    
08.12.2017 / 11:42