# Please help me understand arrays

**URL:** <https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015>\
**Category:** Scribe\
**Created:** [September 10, 2020, 10:14am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015 "2020-09-10T10:14:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![foxfire](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/foxfire/32/13755_2.png) [@foxfire](https://discourse.codecombat.com/u/foxfire)\
**Post date:** [September 10, 2020, 10:14am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/1 "2020-09-10T10:14:07Z")

</div>

i don’t understand how arrays work (the kind that you use to see which enemy has least health, which coin is farthest, etc.). can someone please explain?  
i can’t figure out alone how to win all the levels in the desert and use this forum a lot (it’s not a bad thing, i just want to know it myself…) 🥺

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [September 10, 2020, 10:42am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/2 "2020-09-10T10:42:47Z")

</div>

Hi foxfire,

Good question! An array is just a list. It can be a list of numbers, or words, or enemies, or lots of other things. It is contained by square brackets [], and usually - but not always - the elements are separated by commas.

The first element in an array is labelled 0, the second is labelled 1 etc. So you can call the first element with array[0].

I find that sometimes it helps to see the array, and in CoCo you can generally just put in hero.say(array); or hero.say(array[5]) if you want to see the sixth element.

Does that answer your question, or do you want more information?

Jenny

---

<div class="post-metadata">

**Author:** ![foxfire](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/foxfire/32/13755_2.png) [@foxfire](https://discourse.codecombat.com/u/foxfire)\
**Post date:** [September 10, 2020, 11:30am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/3 "2020-09-10T11:30:06Z")

</div>

@jka2706, my problem is with using arrays in a code.  
for example:

```python
while(true) {
    var coins = hero.findItems();
    var coinIndex = 0;
    var nearest = null;
    var nearestDistance = 9999;
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        var distance = hero.distanceTo(coin);
        if (coin.distance < nearestDistance) {
            nearest = coin;
            nearestDistance = coin.distance;
        }
    }
    if (nearest) {
        hero.moveXY(nearest.pos.x, nearest.pos.y);
    }
}

```

the hero just stands there and doesn’t move…  
it happens to me in all array levels, can you see what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [September 10, 2020, 11:35am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/4 "2020-09-10T11:35:55Z")

</div>

> [@foxfire](#):
>
> `coinIndex++;`

What happens if you put this at the end of the second while loop?

Andrei

---

<div class="post-metadata">

**Author:** ![foxfire](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/foxfire/32/13755_2.png) [@foxfire](https://discourse.codecombat.com/u/foxfire)\
**Post date:** [September 10, 2020, 11:54am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/5 "2020-09-10T11:54:00Z")

</div>

sorry, it doesn’t work…

---

<div class="post-metadata">

**Author:** ![foxfire](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/foxfire/32/13755_2.png) [@foxfire](https://discourse.codecombat.com/u/foxfire)\
**Post date:** [September 10, 2020, 12:01pm UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/6 "2020-09-10T12:01:52Z")

</div>

I saw that it was supposed to be

```python
distance

```

and not

```python
coin.distance

```

but it still wouldn’t work…  
any other ideas?

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [September 10, 2020, 12:55pm UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/7 "2020-09-10T12:55:49Z")

</div>

Maybe you didn’t replace all "coin.distance"s with “distance”…  
this will work:

```python
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        var distance = hero.distanceTo(coin);
        if (distance < nearestDistance) {
            nearest = coin;
            nearestDistance = distance;
    }

```

and this is also OK:

```python
    while(coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        coin.dist = hero.distanceTo(coin);
        if (coin.dist < nearestDistance) {
            nearest = coin;
            nearestDistance = coin.dist;
        }
    }

```

Don’t use coin.distance, you’ll get this error:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/d/d7b55e749c214a6c545002c5af76aa9021398037.png)

---

<div class="post-metadata">

**Author:** ![foxfire](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/foxfire/32/13755_2.png) [@foxfire](https://discourse.codecombat.com/u/foxfire)\
**Post date:** [September 10, 2020, 4:00pm UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/8 "2020-09-10T16:00:55Z")

</div>

works perfectly. thank you! 💟  
I guess my arrays problems where because I didn’t place my coinIndex++ in the right place.  
thanks for the explanation of arrays and level help ❗

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [September 10, 2020, 10:24pm UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/9 "2020-09-10T22:24:06Z")

</div>

@jka2706  
in an array _usually - but not always - the elements are separated by commas_  
I suppose you are right - can you show an array where the elements aren’t separated by commas ( I suck more at python than in java script)

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [September 11, 2020, 7:24am UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/10 "2020-09-11T07:24:57Z")

</div>

I’m just going off the instructions for [https://codecombat.com/play/level/the-spy-among-us](https://codecombat.com/play/level/the-spy-among-us).

They include:

```python
// This function checks for a specific letter in a word.
// A string is just an array! Loop over it like an array

```

Instructions the same in JS and python. No commas 🙂.

Jenny

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [September 11, 2020, 12:47pm UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/11 "2020-09-11T12:47:22Z")

</div>

Apparently pets know arrays and coding better than me 🙂

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/5/5ada00a9bf662d261834161ed00e8ee10fd1bb0d.jpeg)

---

<div class="post-metadata">

**Author:** ![foxfire](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/foxfire/32/13755_2.png) [@foxfire](https://discourse.codecombat.com/u/foxfire)\
**Post date:** [September 11, 2020, 1:46pm UTC](https://discourse.codecombat.com/t/please-help-me-understand-arrays/24015/12 "2020-09-11T13:46:23Z")

</div>

Hahaha! 😆 smart cougar 🐅
