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;
    }
}
3
  • maybe the problem lies in appendSectionQuestions? Commented Jan 22, 2015 at 22:02
  • 3
    Try declaring x 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 lengths compare. Commented Jan 22, 2015 at 22:06
  • @JonathanLonowski Good call, that's probably what it is. Commented Jan 22, 2015 at 22:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.