Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[LEVEL HELP!] CS5 Toil and Trouble Javascript

I am not receiving any errors in my code, but for some reason the soldiers are just ignoring the witches and attacking the nearest enemy instead. Here’s my code:

hero.commandArcher = function(archer) {
    // Soldiers should attack enemies.
    var enemy = archer.findNearestEnemy();
    if (enemy) {
        if (archer.distanceTo(enemy) < 10) {
         hero.command(archer, "attack", enemy);   
        }
    }
};

hero.commandSoldier = function(soldier) {
    // Soldiers should attack enemies.
    var enemies = hero.findEnemies();
    var ogre = hero.findByType("ogre");
    var thrower = hero.findByType("thrower");
    var witch = hero.findByType("witch");
    var nearestEnemy = hero.findNearest(witch);
    if (enemies) {
     if (witch) {
      hero.command(soldier, "attack", witch);   
     }
    }
};

while(true) {
    var friends = hero.findFriends();
    var enemies = hero.findEnemies();
    var witch = hero.findByType("witch");
    for(var i=0; i < friends.length; i++) {
        if (friends.type == "soldier") {
        // Use your chooseTarget function to decide what to attack.
        hero.commandSoldier(friend);
    }
     if (friends.type == "archer") {
        hero.commandArcher(friend);
    }
}
}

I don’t know if this matters but this is on Student levels, so I don’t have any equipped stuff or anything

Hi @Bobby123756, welcome to the CodeCombat Discourse! :tada:
You’ve defined witch as an array:

Now you attack it:

Can you attack an array? Think not. Why not use the variable you made:

But, thinking about it, I think the real problem is that you can’t define hero. do something commands with functions in codecombat. You might have to do:
function commandArcher(archer) {
Instead.
Danny