0

I've been trying to create list of months where id of every months has id written from months_id...

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var months_id = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
var cells = "";

for (i = 0; i <= 11; i++) {
        cells += "<div id='" + months_id + "'>" + months[i] + "</div>";
}
document.getElementsByClassName("divMonths")[0].innerHTML = cells;

I've tried for (i = 0 && months_id = 0; i <= 11 && months_id <= 0; i++ && months_id++)

and for (i = 0; i <= 11; i++) { for (months_id = 0; months_id <= 11; months_id++) {//...} }

but both methods didn't work.

Is there any way how to write it correctly?

J.Stef
  • 17
  • 1
  • 1
    What was wrong with your second approach? What errors did you get? Please describe in detail what you mean by "didn't work". And think about whether using the same variable name in the same scope is a good idea or not. – mason Nov 21 '19 at 18:06
  • 2
    In your 2nd approach what do you think would happen to the value of `i` in the inner loop and how do you think that could influence the outer loop? – Igor Nov 21 '19 at 18:07
  • Yes: `for` `for` – Tim Biegeleisen Nov 21 '19 at 18:11
  • @Igor Sorry, I meant **months_id** instead of **i**. It's updated. – J.Stef Nov 21 '19 at 18:11
  • Notice that you define `months_id` as an array but are using it as a number. Does that help you spot the bug? – Matt Johnson-Pint Nov 21 '19 at 18:13
  • Why do you need two for loops? Just one loop can let you write the correct month and month_id to your element's innerHTML. Also, months_id in the loop at the top should be months_id[i] – ecg8 Nov 21 '19 at 18:13
  • @ecg8 I know I can have just one loop but I need the id to be a double digit number. That's the problem. – J.Stef Nov 21 '19 at 18:16
  • @mason Every month was written 12 times. (12× January, 12× February, etc.) – J.Stef Nov 21 '19 at 18:30

2 Answers2

0

This is your problem:

var months_id = //...
//...
for (months_id = 0; months_id <= 11; months_id++) {//...}

You're using months_id twice in the same scope for two different variables. In other words, there is no way for the program to identify if your months_id in the inner loop (the "second for"... inner for, you see?) is referring to your array or to the iterator variable.

Your second approach is correct, but you must rename either the array or the inner iterator. Usually, i, j and k are used as iterator variables. Take a look at this question for further information. Long story short, your code should go like this:

for (i = 0; i <= 11; i++) {
    for (j = 0; j <= 11; j++) {//...}
//...
}

Not really sure what you're trying to do here, but that's the correct syntax for nested loops, which is what your question refers to.

Edit: if you dig reading about why you should name your iterator variables like that, this answer gets quite creative

makoshichi
  • 2,310
  • 6
  • 25
  • 52
-2

You need to create a div in which you are writing the variable 'cells'.

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var months_id = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
var cells = "";
document.writeln("<div class='divMonths'></div>")
for (i = 0; i <= 11; i++) {
        cells += "<div id='" + months_id + "'>" + months[i] + "</div>";
        //document.writeln(months[i])
}
document.getElementsByClassName("divMonths")[0].innerHTML = cells;
Sheri
  • 1,383
  • 3
  • 10
  • 26