0

Why is this loop never-ending? Every section may or may not have subsections, which are the same class as a section.The for statement is working very strangely. It goes straight back up to list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>'); all the time. If there are no subsections it should not do that.

function createAssessmentSectionFormHTML(section) {
    var list = $('#assessmentSectionForm' + platform);
     appendSection(section, list);
}

function appendSection(theSection, list) {
    list.append('<label class="heading">' + theSection.description + '</label><br/><hr><br/>');
    appendSectionQuestions(theSection, list);
    if (theSection.allSubSections) {
        for (x = 0; x < theSection.allSubSections.length; x++) {
            var theSectionA = theSection.allSubSections[x];
            appendSection(theSectionA, list);
        }
        return;
    }
}
btf
  • 173
  • 2
  • 11
  • maybe the problem lies in `appendSectionQuestions`? – Daniel Weiner Jan 22 '15 at 22:02
  • 3
    [Try declaring `x`](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it) so it's local to `appendSection()`. Currently, it's a global that's shared between every call to `appendSection()` and may effectively rewind or fast-forward a previous round depending on how the `length`s compare. – Jonathan Lonowski Jan 22 '15 at 22:06
  • @JonathanLonowski Good call, that's probably what it is. – Daniel Weiner Jan 22 '15 at 22:08

1 Answers1

2

I believe you are missing a "var" in your for loop:

for (var x = 0; x < theSection.allSubSections.length; x++) {

This should solve the problem - it's running infinitely because x is not declared. Undefined is always less than any length, so there's no end point.