Toil and Trouble

I have a confession to make…

This level almost makes me want to give up. I just really do not understand what to do and what i am doing

function chooseTarget(){
    var friend = hero.findNearest(hero.findFriends());
var enemy = hero.findNearest(hero.findEnemies());
if (friend.type == "solider" && enemy.type =="witch"){
    return "witch";
    }if(friend.type == "archers" && enemy.type !="witch"){
        return "archers";
        }

}
while(true) {
    var friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        // Use your chooseTarget function to decide what to attack.
        var friend =friends[i];
        var target = chooseTarget();
        if(target == "witch"){
            hero.command(friend,'attack',enemy);
            }
        if(target == "archers"){
            hero.command(friend,'attack',enemy);
            }    
}
}

1 Like

before this level, you were taught on how to use the for loops for commanding friends. so then you need the line for friend in friends: were also told in the level for the soldiers to attack the witches and that the archers should attack the nearest enemy. the archers will not ever be a enemy in this level. you also didn’t check if the enemy exists. The archers should use the code enemy = friend.findNearest(hero.findEnemies()) while the soldiers should use the code enemy = friend.findNearest(hero.findByType("witch"))