0

I'm doing an example of chart with chart JS. In the horizontal line, I'm showing four months to show the evolution of an indicator. I've an array of months composed this way. As you see each month with a key. My problem is that I don't' know how to loop when I arrive at the index n-3 = 0 to start again from the key 11

var date  = new Date();
var month = new Array();

month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

[month[n-3],month[n-2], month[n-1], month[n]]

@evolutionxbox I displayt he months like this 11 (december) 0 (january) 1 (February) 2. In code is illustrated like this month[n-3],month[n-2], month[n-1], month[n] . in this case n-3 = -1 i don't this key so i should return to 11.

So can anyone help me please. Thanks

robertklep
  • 198,204
  • 35
  • 394
  • 381
KubiRoazhon
  • 1,759
  • 3
  • 22
  • 48
  • [arr.reverse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) and [Creating an array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Creating_a_two-dimensional_array) – Emil S. Jørgensen Jun 20 '16 at 10:34
  • What does **"I don't' know how to loop when I arrive at the index n-3 = 0 to start again from the key 11"** mean? Could you explain it a bit more? - [Check here for help on looping arrays](http://stackoverflow.com/a/9329476/989920) – evolutionxbox Jun 20 '16 at 10:34
  • @evolutionxbox I displays the month 11 (december) 0 (january) 1 (February) 2. In code is illustrated like this month[n-3],month[n-2], month[n-1], month[n] . in this case n-3 = -1 i don't this key so i should return to 11. – KubiRoazhon Jun 20 '16 at 10:37
  • @KubiRoazhon umm.. (n-3)%12, (n-2)%12, and so on ... is this what you are asking? – Quirk Jun 20 '16 at 10:38
  • What? Give us an example output of what you want – Starfish Jun 20 '16 at 10:38
  • `month.reverse().forEach(function(monthName) { console.log(monthName); })` - this will output the names of the months in reverse order – evolutionxbox Jun 20 '16 at 10:38

1 Answers1

1

Use the % Operator: If you increment n just use n%12. That way you start at 0 after 11+1=12.

In your case:

[month[(n-3)%12],month[(n-2)%12], month[(n-1)%12], month[n%12]]

var month = new Array();

month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

for(n =3;n< 24; n++){
 console.log(month[(n-3)%12]+","+month[(n-2)%12]+","+month[(n-1)%12]+","+month[n%12])
}
Urknecht
  • 415
  • 10
  • 16
  • Could you add an example? Why is the remainder operator better than reversing/looping over the array? – evolutionxbox Jun 20 '16 at 10:41
  • I think looping over the array is a good idea, but as I understand the question he wants to increment n and get started again at 0 for values bigger than 11. That is why i proposed the Modulus Operator. – Urknecht Jun 20 '16 at 10:44
  • I personally would turn it around and add to n and don't substract, but since you stated your question like that I wrote the code like this. Why don't you start with n, n+1,n+2,n+3? – Urknecht Jun 20 '16 at 10:57