Please help me understand arrays

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…) :pleading_face:

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

5 Likes

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

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?

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

Andrei

sorry, it doesn’t work…

I saw that it was supposed to be

distance

and not

coin.distance

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

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

    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:

    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

2 Likes

works perfectly. thank you! :heart_decoration:
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 :exclamation:

4 Likes

@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)

1 Like

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

They include:

// 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 :slight_smile:.

Jenny

4 Likes

Apparently pets know arrays and coding better than me :slight_smile:

5 Likes

Hahaha! :laughing: smart cougar :tiger:

1 Like