0

This loop works 100% fine:

 for(m = 1; m < splitData.length; m++)

This one however throws errors! (Relating to loop body)

for(m = splitData.length; m > 1; m--)

The entire chunk of code is:

// Success
if (splitData[0] == "1") {
    // DbID, username, msg, date

 for(m = splitData.length; m > 1; m--){
        var splitMsg = splitData[m].split("¬");
        $('#<%=discussionBoard.ClientID %>').prepend('<div class="messageWrapper">
             <div class="messageHead">' + splitMsg[1] + '</div>
             <div class="messageTxt">' + splitMsg[2] + 
             '<div class="messageDetails">' + splitMsg[3] + 
             '</div></div></div>');
 }

The first index in splitData is just a 1 or 0 indicating if Ajax returned good data. I then want to loop through the rest of the input.

3 Answers 3

4

You're off-by-one. Remember that the indexes in Javascript are 0-based, which means that they go from 0 to length - 1. Try this instead:

for (m = splitData.length - 1; m >= 1; m--)
Sign up to request clarification or add additional context in comments.

4 Comments

You dont want to check for >= to 0 just >
@Raynos: I didn't notice that he wanted to ignore the first element. So, yes, I was off-by-one. :)
To be the exact reverse of his original loop should it not be m > 0 not m >= 0?
The most important part would be "m = splitData.length - 1"
2

Have you tried

for(m = splitData.length - 1; m > 0; m--)

instead of:

for(m = splitData.length; m > 1; m--)

Comments

1

// count from 1 to splitData.length - 1

for(m = 1; m < splitData.length; m++)

// count from splitData.length to 2

for(m = splitData.length; m > 1; m--)

you want

for (m =splitData.length = 1; m > 0; m--)

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.