Set an array to circular in Javascript

Asked

Viewed 263 times

0

How could I give a loop in array? The code takes the current day and adds a value x. For example, I would like when you get to Monday where the script

document.getElementById("g").innerHTML = (dias[now.getDay() + 6] + ", " + [now.getDate () + 6] +  " de " + meses [now.getMonth() ]);

(it will add Monday plus 6 days), but it gives error, because there will be only 5 more days in the array. So what I wanted was for him to create a loop.

var now = new Date();
var dias = ["DOMINGO","SEGUNDA","TERÇA","QUARTA","QUINTA","SEXTA","SABADO"];
var meses = ["JAN", "FEV", "MAR", "ABR", "MAI", "JUN", "JUL", "AGO", "SET", "OUT","NOV", "DEZ"];
document.getElementById("a").innerHTML = (dias[now.getDay()] + ", " + now.getDate () + " de " + meses [now.getMonth() ]);
document.getElementById("b").innerHTML = (dias[now.getDay() + 1] + ", " + [now.getDate () + 1] + " de " + meses [now.getMonth() ]);
document.getElementById("c").innerHTML = (dias[now.getDay() + 2] + ", " + [now.getDate () + 2] + " de " + meses [now.getMonth() ]);
document.getElementById("d").innerHTML = (dias[now.getDay() + 3] + ", " + [now.getDate () + 3] + " de " + meses [now.getMonth() ]);
document.getElementById("e").innerHTML = (dias[now.getDay() + 4] + ", " + [now.getDate () + 4] + " de " + meses [now.getMonth() ]);
document.getElementById("f").innerHTML = (dias[now.getDay() + 5] + ", " + [now.getDate () + 5] + " de " + meses [now.getMonth() ]);
document.getElementById("g").innerHTML = (dias[now.getDay() + 6] + ", " + [now.getDate () + 6] +  " de " + meses [now.getMonth() ]);
  • 5

    It wasn’t clear what you want to do, but have you thought about taking the rest of the split by 7? So, if the sum result exceeds 6, it goes back to the top of the list.

  • Well that I would like to do, but how would I do that? What should I use? How should I do it? If you can help me I would really appreciate it.

  • As I said, it’s not clear what you want to do. If you can [dit] the question and try to be clearer, it might be possible. Explain, for example, what the code should do and who all these elements are in the DOM;

1 answer

3

Answering the main question, you cannot literally define an array as circular, but you can traverse it in a circular way using modular arithmetic. This is easily reachable in Javascript using the module operator (%) and a little calculation with it.

Given an array arr long n and a value val stored in the array and that will be obtained through an index i, the most secure, circular way to access the array, disregarding whatever the value and sign of i, would be:

let val = arr[(i % n + n) % n];

This small calculation is necessary because Javascript always evaluates the module operation as the remnant division between the dividend (first operand) and the divisor (second operand) disregarding its signals, but attributing to the rest the dividend signal. This behavior does not always result in the desired "loop" effect of modular arithmetic, and may result in incorrect access of a negative array position.

For example, given i = -5 and n = 3, Javascript would evaluate the expression -5 % 3 in -2, which is incorrect and would result in an array access error. The correct modular arithmetic would be the expression being evaluated in 1, what is obtained by Javascript with the calculation of the expression (-5 % 3 + 3) % 3.

For the purpose that you were wanting and showed partially in code, you could even use the module operation directly -- as Anderson mentioned when he suggested "take the rest of the division". The solution proposed here is only more generic and more secure, as it is not guaranteed (asserted) by your code that the final value of the access index to the array will always be positive, although now you are relying on functions from the standard language library.

References and more information:

  1. https://pt.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic
  2. https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic (same content as the previous link, but in English, with more comments from the teacher and students)
  3. https://en.wikipedia.org/wiki/Modular_arithmetic
  4. https://en.wikipedia.org/wiki/Modulo_operation
  5. https://dev.to/maurobringolf/a-neat-trick-to-compute-modulo-of-negative-numbers-111e

Browser other questions tagged

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