Mountain Mercenaries

When I run this code (JS) the error message says that the hero place holder needs something to command


// Gather coins to summon soldiers and have them attack the enemy.

loop {
    // Move to the nearest coin.
    var item = this.findNearest(this.findItems());
    var x = item.pos.x;
    var y = item.pos.y;
    this.moveXY(x, y);
    // If you have funds for a soldier, summon one.
    if ( > this.costOf("soldier")) {
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
        // Loop over all your soldiers and order them to attack.
        var soldiers = this.findFriends();
        var soldierIndex = 0;
        var soldier = soldiers[soldierIndex];
            this.command(soldiers, "attack", enemy);
        // Use the 'attack' command to make your soldiers attack.
        //this.command(soldier, "attack", enemy);

You are trying to command the array of soldiers not the individual…

Oh my bad thanks Vlevo

Okay this question is only here because I cannot use arrays to save my life but do I have to value of a certain place of the array. What is the syntax for getting the value of an array in JS

I’m not sure I understand the question.
You have var soldier = soldiers[soldierIndex]; in your code. It takes the soldierIndex-th(starting from 0) element of soldiers array. So soldiers[0]; takes 1st element of soldiers array. Do not try to access non-existent elements of array. Use soldiers.length to get the size of array. Then as far as I know first element is 0 and last soldiers.length-1.

There at least one other error, which in a sense, brokenelevator is alluding to…
You have no “loop” around your use of soldiers (he spoke on the subject of not going off the end of the array)…

I was just answering the question given, “why it says the error”

var soldier = soldiers[soldierIndex];
    this.command(soldiers, “attack”, enemy);

soldiers (is best to name your arrays in the plural, since it is supposed to be (usually) holding more than one thing) are all the “array”. soldier (one thing is singular) is the individual . . . so the “if” and the “command” need to use the individual. (hurray, for a language with singulars and plurals.)

and yes, javascript is “zero- indexed” (meaning zero to length(array)-1) (say length(array) = five. Then there are five elements: 0, 1, 2, 3, 4.) Problems with forgetting your “0” are sometimes refered to as “fence-post” problems . . . If you build a fence with four spans “= = = =” how many posts “|” do you need? You start with a post (0) “|” and add spans 1-4 “=|” making a fence “|=|=|=|=|”. If you forget the starter post (0), well that end doesn’t work very well… “=|=|=|=|” and your fence might fall over “___/=|=|” :smile: at the very least the animals will escape " :pig2: =|=|"



That is a fun way of explaining javascript(and any other language) being zero-indexed. :smile:

I am aware of the fence-post problem.

Oh yeah and @nick there is a problem with the error message going right next the contact list and making it so I cannot see the error
And I have figured out the problem with my code but I just need one piece of syntax, how do I loop over an array?

Really, you got this far with out doing that?

Go back and redo “Sarven Savior” it is in the desert up towards the top.

Did you skip: Sarven Savior, Lurkers, Sarven Shepard, Shine Getter, and Mad Maxer. All of those Free levels are loop over array levels…