Toil and Trouble [JAVASCRIPT]

can someone give me the details on how to do this? i am so lost

// Ogre Witches have some unpleasant surprises ready for you.

// Define a chooseTarget function which takes a friend argument
// Returns the a target to attack, depending on the type of friend.
// Soldiers should attack the witches, archers should attack nearest enemy.
function chooseTarget(friend){
        var nearestWitch = friend.findNearest(hero.findByType("witch"));
        if(friend && friend.type == "soldier") {
            hero.command(friend, "attack", nearestWitch);
        }
    }
while(true) {
    var friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        // Use your chooseTarget function to decide what to attack.
        chooseTarget();
    }
}

Your code looks good so far, just missing one thing.
// Soldiers should attack the witches, archers should attack nearest enemy.
You have the first half of it already, it’s just missing the code for the archers to attack their nearest enemy. An optional part to add would be commanding the soldiers to attack their nearest enemy if there are no witches alive.

1 Like