[SOLVED]Help With Library tactician JavaScript

function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {
        x: 41,
        y: 40
    };
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}

This is my commandSoldier func.

Im not quite sure if converting my code will help, the levels look completely different. Im gonna stick to JavaScript

ok, listen to @sci12 , I’m bad at javascript.Sorry :no_mouth:

okay, sure thanks for the help tho! I really appreciate it

1 Like

Even with your commandSoldier function it did not work

This is NOT how you write a for loop!
I am not giving you the answer to this loop yet.
Look at other for loops in the level to learn how to setup the for loop.

you better do simpler javascript levels, than this one.This way you’ll learn javascript

I don’t get what’s wrong with it, sorry I have not coded JavaScript in a while

There is no “in” the for loop requires 3 parts variable, comparison, increment
look

for(var i = 0; i < enemies.length; i++){}
1 Like

++ means +1 to iteration variable

But now they don’t return to hushbaum

/// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!

// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {
        x: 41,
        y: 40
    };
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}

// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = hero.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i=0; i < enemies.length; i++) {
        if (enemy && enemy.health > 15) {
            bestTarget = enemy;
        }
    }
    // Only focus archers' fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}


// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
    var friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        if(friend.type == "archer"){
            var archerfindNearestEnemy = friend.distanceTo(enemy) < 15;
        }
    
        var nearest = archerfindNearestEnemy;
        if(archerTarget) {
            hero.command(archer, "attack", archerTarget);
        }   
        else if(nearest) {
        hero.command(archer, "attack", nearest);
        }
    }
}

var archerTarget = null;
while(true) {
    // If archerTarget is defeated or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget();
    }
    var friends = hero.findFriends();
    var soldiers = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archers = hero.findByType("archers");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    for (var q=0; q < archers.length; q++){
        var archer = archers[q];
        commandArcher(archer);
}
    
}

Also you if statement is wrong. (not in syntax)

  if (enemies[i].health > mostHealth) {
            mostHealth = enemies[i].health;
            bestTarget = enemies[i];
        }

Use this for your if.

alse remove apostrof(’) here

//only focus archers'

I have a feeling there is a problem here:

    // Only focus archers fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}

No, that is correct.

oh, okay, I was just wondering, because my soldiers still attacked the ogres

I updated my code and now it looks like this:

/// Hushbaum has been ambushed by ogres!
// She is busy healing her soldiers, you should command them to fight!
// The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!

// Soldiers spread out in a circle and defend.
function commandSoldier(soldier, soldierIndex, numSoldiers) {
    var angle = Math.PI * 2 * soldierIndex / numSoldiers;
    var defendPos = {
        x: 41,
        y: 40
    };
    defendPos.x += 10 * Math.cos(angle);
    defendPos.y += 10 * Math.sin(angle);
    hero.command(soldier, "defend", defendPos);
}

// Find the strongest target (most health)
// This function returns something! When you call the function, you will get some value back.
function findStrongestTarget() {
    var mostHealth = 0;
    var bestTarget = null;
    var enemies = hero.findEnemies();
    // Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for(var i=0; i < enemies.length; i++) {
        if (enemies[i].health > mostHealth) {
            mostHealth = enemies[i].health;
            bestTarget = enemies[i];
        }
    }
    // Only focus archers fire if there is a big ogre.
    if (bestTarget && bestTarget.health > 15) {
        return bestTarget;
    } else {
        return null;
    }
}


// If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
function commandArcher(archer) {
    var friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        if(friend.type == "archer"){
            var archerfindNearestEnemy = friend.distanceTo(enemy) < 15;
        }
    
        var nearest = archerfindNearestEnemy;
        if(archerTarget) {
            hero.command(archer, "attack", archerTarget);
        }   
        else if(nearest) {
        hero.command(archer, "attack", nearest);
        }
    }
}

var archerTarget = null;
while(true) {
    // If archerTarget is defeated or doesn't exist, find a new one.
    if(!archerTarget || archerTarget.health <= 0) {
        // Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget();
    }
    var friends = hero.findFriends();
    var soldiers = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archers = hero.findByType("archers");
    for(var i=0; i < soldiers.length; i++) {
        var soldier = soldiers[i];
        commandSoldier(soldier, i, soldiers.length);
    }
    // use commandArcher() to command your archers
    for (var q=0; q < archers.length; q++){
        var archer = archers[q];
        commandArcher(archer);
}
    
}


If your code is right try submitting a few times

remove the apostrof there too

it does not work. it is incorrect