Library tactician [javascript] need help

ok so there doesn’t seem to be any issues with the code but a soldier keeps dying 30 seconds in. is there something i could do to improve this

// 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.lenght; ++i)
    {
        if(mostHealth < enemies[i].health)
        {
            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 nearest = archer.findNearestEnemy();
    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 i=0; i < archers.length; i++) {
       
        commandArcher(archers[i],archerTarget);
    }    
}

the code is also red for some reason. whats with that

The easy one, it is red because of the ’ in the comments. Add the word javascript after your first set of back ticks and it will prevent the apostrophe from triggering the color change.

//does'nt change to red with javascript on the same line of first back ticks
//doesn't have javascript after the back ticks

A few typos in your code.

for(var i =0;i < enemies.lenght; ++i) // in findStrongestTarget()
var archers = hero.findByType("archers"); // archers in findByType should not be plural

that actually shorted the time of the code so now the soldiers only last 20 or so seconds
here is what i got now

// 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.lenght; ++i)
    {
        if(mostHealth < enemies[i].health)
        {
            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 nearest = archer.findNearestEnemy();
    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 soldier = hero.findByType("soldier");
    // Create a variable containing your archers.
    var archer = hero.findByType("archer");
    for(var i=0; i < soldier.length; i++) {
        var soldier = soldier[i];
        commandSoldier(soldier, i, soldier.length);
    }
    // use commandArcher() to command your archers
    for(var i=0; i < archer.length; i++) {
        commandArcher(archer[i],archerTarget);
}
}

Sorry, I didn’t fix it for you, I was just showing you where it was. That line is a typo that you need to fix.

for(var i =0;i < enemies.lenght; ++i) //length is spelled wrong

Also, I didn’t mean you needed to change the variable names, but the type in the method.

hero.findByType("archer"); //   archer is a specific type and can't be plural

Because you changed all the variables to singular, it is messing up your for loop since your code doesn’t know what variable you are referring to in these lines. One of the dangers of Python and the dynamic variables.

var soldier = hero.findByType("soldier"); // soldier is an array of soldiers
    for(var i=0; i < soldier.length; i++) {
        var soldier = soldier[i]; // now soldier is one object
        commandSoldier(soldier, i, soldier.length); // still one object, but you are treating it like an array for the length

thank you so much i have been stuck on this level for a bit. sorry about misinterpreting the first post. its surprising how a single letter can fail a whole code