My code calls all of the friends back home but, the time when the last one is called does not give it enough time to get back to the base and the last ogre to get killed. Is there an issue with the code or is this a bug?
Code is in Javascript. while (true) {
** var weakestFriend = null;**
** var leastHealth = 9999;**
** var friendIndex = 0;**
** // Find which friend has the lowest health.**
** var friends = hero.findFriends();**
** while (friendIndex < friends.length) {**
** var friend = friends[friendIndex];**
** if (friend.health < leastHealth && friend.health != 30) {**
** leastHealth = friend.health;**
** weakestFriend = friend;**
** }**
** friendIndex += 1;**
** }**
** // Tell the weakest friends to go home first.**
** if (weakestFriend) {**
** hero.say("Hey " + weakestFriend.id + ", go home!");**
** }**
**}**
friend.health != 30 - is too strict
if archer take any damage hero will calling him
Until health of the peasant not drop lower than archer
And it is lower than 30
So level for sure going to end faster
Than ogre do enought damage for
weakestFriend variable take peasant instead of archer
You should also take into accaunt
hero.say takes 7 seconds for this level.
You pretty much run through every friend and compare their health.
Here’s a similar instance that compares the number of apples on the trees in an apple tree grove.
trees = farmer.findTrees()
treeIndex = 0
lowAppleCount = 99999
lowTree = None
while treeIndex < len(trees):
tree = trees[treeIndex]
if tree.appleCount < lowAppleCount:
lowAppleCount = tree.appleCount
lowTree = tree
treeIndex += 1
farmer.say(lowTree + " has the least apples.")