0

The code is meant to generate an calendar using javascript and tables. The months and days are in dutch but it should be understandable. Somehow this end in an endless loop while it should stop when days is bigger or the same as the length of my array.

It's supposed to make 12 tables for each month and then fill these up with the days of that month. It's not finished yet since it'll presume that every day will start at sunday. I think I should also note that I'm not that experienced yet.

var months = new Array();
months[0] = "januari";
months[1] = "februari";
months[2] = "maart";
months[3] = "april";
months[4] = "mei";
months[5] = "juni";
months[6] = "juli";
months[7] = "augustus";
months[8] = "september";
months[9] = "oktober";
months[10] = "november";
months[11] = "december";

var maxDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var weekDays = ["zo", "ma", "di", "wo", "do", "vr", "za"];
var kalender = 0;
var days = 1;
var weekDaysNumber = 0;


while(kalender < months.length){
    document.write("<table><tr>");
    document.write("<th>" + months[kalender] + "</th></tr>");
    document.write("<tr>");
    while (weekDaysNumber < 7){
        document.write("<td>" + weekDays + "</td>";
        weekDaysNumber++;
    }
    document.write("</tr>");
    while(days < maxDays[kalender]){
        document.write("<td>" + days + "</td>");

        var rows = 0;
        if(rows == 7 && days !== maxDays[kalender]){
        document.write("</tr>");
        document.write("<tr>");
        rows = 1;
        }

        rows++;
        days++;

    }

    if(days == maxDays[kalender]){
            document.write("</tr></table>");
            kalender++;
            weekDaysNumber=0;

        }

}

2 Answers2

2

Because you never reset the days variable to 1, and so on the second outer loop, it's never equal to maxDays[kalendar]. There may well be other issues, the code looks much more complex than necessary, but that's the glaring reason for the infinite loop: When days reaches 31, you break the first loop, start the second, and since 31 is greater than 28 the inner loop is never entered and your == maxDays[kalendar] is never entered.


Your browser has a fully-featured debugger built into it. You can use that to set breakpoints, single-step through code, inspect variables, etc.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
0

You need to reset days back to 1 inside the while(kalender < months.length){ loop.

NPE
  • 438,426
  • 93
  • 887
  • 970