Steelclaw Gap Javascript Help [Solved]

// This level introduces the % operator, also known as the modulo operator.
// a % b returns the remainder of a divided by b
// This can be used to wrap around to the beginning of an array when an index might be greater than the length

var defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}];

var summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"];

// You start with 360 gold to build a mixture of soldiers and archers.
// this.built is an array of the troops you have built, ever.
// Here we use "this.built.length % summonTypes.length" to wrap around the summonTypes array
function summonTroops() {
    var type = summonTypes[hero.built.length % summonTypes.length];
    if(hero.gold >= hero.costOf(type)) {
        hero.summon(type);
    }
}

function commandTroops() {
    var friends = hero.findFriends();
    for(var friendIndex=0; friendIndex < friends.length; friendIndex++) {
        var friend = friends[friendIndex];
        // Use % to wrap around defendPoints based on friendIndex
       var defendIndex = friendIndex % defendPoints;
        // Command your minion to defend the defendPoint
        hero.command(friend, "defend", defendIndex);
    }
}

while(true) {
    summonTroops();
    commandTroops();
}

error on this line

        hero.command(friend, "defend", defendIndex);

is “unhandled error: ‘defend’ takes no arguments.Must defend a target or an {x,y} position.”
Link:

What do I do to fix this?
Thanks

Maybe do the line like this. I don’t know much about javascript so I might need somebody to traslate.

hero.command(friend, "defend", defendIndex[friendIndex % len(defendPoints)]);
2 Likes

I think it would be this in JS:

hero.command(friend, "defend", defendPoints[friendIndex % defendPoints.length])

So try this and see if it works. Or, you could keep the variable and do it like this:

var defendIndex = defendPoints[friendIndex%defendPoints.length]
hero.command(friend, "defend", defendIndex)
2 Likes

Thanks for the tips, I passed the level! :smile:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.