1

I am having an issue with a javascript for loop. I am adding up the elements of an array, but for some reason my loop adds in the first entry twice! There was a similar topic on here before (http://stackoverflow.com/questions/3121670/for-loop-repeats-first-loop-twice) but the author didn't go into his resolution in detail, just that it was "somethin stupid" he did. Can anyone tell me what I'm doing stupid??

for(j=0;j<ARRAY.length;j++)
{TOTAL += ARRAY[j];}

The output is used in a HTML table and it is displaying correctly, it's just the doubled first entry that's the issue!

Any help would be greatly appreciated.

8
  • Well it shouldn't make any difference in this case, but you should make sure that "j" is declared with var. Your loop will definitely not repeat the first iteration. Commented Nov 2, 2011 at 17:13
  • 3
    @Karl Major: also go through some of your previous questions and accept answer that were helpful to you Commented Nov 2, 2011 at 17:15
  • Are the first two entries in ARRAY identical? Commented Nov 2, 2011 at 17:18
  • @Jack Maney - Yes it is. Commented Nov 3, 2011 at 12:15
  • @Pointy - adding var doesn't fix my problem, I had already tried that, hence my utter confusion. Commented Nov 3, 2011 at 12:15

3 Answers 3

2

    var TOTAL = 0;
    for ( var j = 0, len = ARRAY.length; j < len; j++ ) {
        TOTAL += ARRAY[j];
    }

MDN suggest to use a variable to hold the array length. In addition check your scripts with JSLint.

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

2 Comments

Thanks for your response @Bakudan but unfortunately this renders the same result as before! I don't know what's happening. Thanks also for JSLint, I had never heard of this before. in this case though it merely pointed out some formattin errors in my code and produced no remedy for my issue.
@KarlMajor the snippet you've provided is too small to find anything else.
1

make sure you declare with var...

Nice little bit of prompting debugging in there too.

for(var j=0; j < ARRAY.length; j++) {
{
    TOTAL += ARRAY[j];
    //alert("The count of J is now " + j);
}

1 Comment

Thanks for the debugging tip @Graeme but unfortunately adding var does not fix my problem. I am beginning to lose hope. For the time being I have declared a new variable equal to TOTAL-ARRAY[0] which gives the correct figure but this is obviously very messy.
1

Thanks for all of your help. Since my original approach hit a dead end I looked into using a function to do the trick. The following works:

Array.prototype.sum = function() {
for (var j = 0, L = this.length, sum = 0; j < L; sum += this[j++]);
return sum;
}

I then call ARRAY.sum() when creating my html table.

I found the above solution on http://www.codingforums.com/showthread.php?t=218803

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.