# Counting in Loop

**URL:** <https://forum.kirupa.com/t/counting-in-loop/264071>\
**Category:** flash\
**Created:** [June 22, 2008, 11:15pm UTC](https://forum.kirupa.com/t/counting-in-loop/264071 "2008-06-22T23:15:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)\
**Post date:** [June 22, 2008, 11:15pm UTC](https://forum.kirupa.com/t/counting-in-loop/264071/1 "2008-06-22T23:15:47Z")

</div>

Here’s a dumb question: What’s the best way to add up numbers in a loop?

I have four textfields whose heights are:

```auto

17
17
33
50

```

And I want to add these up inside a loop - something like the code below (which doesn’t actually work but you get the idea)

```auto

    for (var i = 0; i < len; i++) {
        var addHeights:Number = 0;
        addHeights += mc.choiceTxt.textHeight;
    }
var bttnPosition:Number = addHeights + 30;

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 22, 2008, 11:58pm UTC](https://forum.kirupa.com/t/counting-in-loop/264071/2 "2008-06-22T23:58:01Z")

</div>

```auto
var addHeights:Number = 0;
var txtArray:Array = [mc.txt1, mc.txt2, mc.txt3];
for (var i:Number = 0; i < txtArray.length; i++) {        
        addHeights += txtArray*.textHeight;
}
var bttnPosition:Number = addHeights + 30;

```

Something like that.

---

<div class="post-metadata">

**Author:** ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)\
**Post date:** [June 23, 2008, 12:16am UTC](https://forum.kirupa.com/t/counting-in-loop/264071/3 "2008-06-23T00:16:48Z")

</div>

Thanks - I should show you the full code loop (this all works except for calculating the button position):

```auto

    for (var i = 0; i < len; i++) {
        var mc = this.path.question_mc.attachMovie("choice_mc", "choice" + i + "_mc", i);
        mc.choiceTxt.htmlText = this.questionArr[this.currentQues].choiceArr*;
        //mc.choiceTxt.autoSize = "left"; // "left" interferes with calculations for button placement
        mc.choiceTxt.autoSize = "true";
        mc.indexNum = i;
        mc._y = yPosition;
        yPosition = yPosition + mc.choiceTxt.textHeight + this.choiceBuffer;
    }

```

Given what you’ve shown me I think I need to code something like:

```auto

        addHeights += this.path.question_mc.choice*_mc.textHeight;

```

ButI know that access operator usage must be wrong.

---

<div class="post-metadata">

**Author:** ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)\
**Post date:** [June 23, 2008, 12:31am UTC](https://forum.kirupa.com/t/counting-in-loop/264071/4 "2008-06-23T00:31:46Z")

</div>

I’ve tried using

```auto
addHeights += this.path.question_mc["choice" + i + "_mc"].choiceTxt.textHeight;

```

but when I trace the addHeights var I get “NaN”.

---

<div class="post-metadata">

**Author:** ![pantas](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pantas](https://forum.kirupa.com/u/pantas)\
**Post date:** [June 23, 2008, 1:50pm UTC](https://forum.kirupa.com/t/counting-in-loop/264071/5 "2008-06-23T13:50:23Z")

</div>

hi there

well i’ll be honnest. cant figure what your problem is, but i hope this helps

```auto

var yPosition:Number = 0
for (var i = 0; i < len; i++) {
        var mc = this.path.question_mc.attachMovie("choice_mc", "choice" + i + "_mc", i);
        mc.choiceTxt.htmlText = this.questionArr[this.currentQues].choiceArr*;
        //mc.choiceTxt.autoSize = "left"; // "left" interferes with calculations for button placement
        mc.choiceTxt.autoSize = "true";
        mc.indexNum = i;
        mc._y = yPosition;
        yPosition += mc._height + this.choiceBuffer;
   }

```

you were not declaring the yPosition variable, probably why it traces NotANumber…  
and in the end, i changed the yPosition to set his value with the hole mc, not just the text.
