Help for Steelclaw Gap [javascript]

hello, I need help for steelclaw gap

I thought I did everything like explained, but I must have missed something. I keep getting an error when I try to command to defend. (see screenshot below).
Thank you for any clue. :wink:

My code is here:

// 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 point = defendPoints[friendIndex.length % defendPoints.length];
        // Command your minion to defend the defendPoint
        hero.command(friend, "defend", point);
    }
}

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

Post in FAQ. I believe it is asking that point needs to be x , y coordinates, and when doing the equation for point, you did not receive a valid value. A way to check is by having the hero say the variable “point”. Could you please post what the answer was?

1 Like

It’s blank… so it seems point is not even calculated… I don’t understand what I did wrong :confused:

1 Like
var point = defendPoints[friendIndex % defendPoints.length];

friendIndex have no length

3 Likes

@htrnblr Nice Catch! You said that friendindex was a number not an array, so there is no length

1 Like

:grin::grin::grin: You did it! thanks a lot!

1 Like

Hi there, I don’t like to do this, but I really need help: Nothing’s working!!!
No errors, but all my soldiers/archers keep dying.
I beg you, please help!

// 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
        hero.command(friend, "defend", defendPoints[friendIndex % 4]);
        // Command your minion to defend the defendPoint
        hero.command(friend, "defend", defendPoints[friendIndex % 4]);
        }
}

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

Why do you need two identical lines of code in commandTroops()? Try first only with a single one.

1 Like

Nope, not working.
Thanks anyway…

What happens? You are losing one warrior?

I also need help with this level. I’m not sure what to put where my code says HELP!

let defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}];
let summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"];
function summonTroops() {
    let type = summonTypes[hero.built.length % summonTypes.length];
    if(hero.gold >= hero.costOf(type)) {
        hero.summon(type);
    }
}
function commandTroops() {
    let friends = hero.findFriends();
    for(let friendIndex=0; friendIndex < friends.length; friendIndex++) {
        let friend = friends[friendIndex];
        // Use % to wrap around defendPoints based on friendIndex
        HELP!
        defendPoints % friendIndex;
        // Command your minion to defend the defendPoint
        hero.command(friend, "defend", defendPoints);
    }
}
while(true) {
    summonTroops();
    commandTroops();
}

pls pls pls please help!

Re-enter the level and carefully read the Goals, especially the last line.

You need to pick a single object from defendPoints, then use it as an argument in hero.command().