Hi this is my first post, sorry if I’m not formatting in the correct way.
I’ve been having some difficulties with the Mad Maxer levels. Could anyone help explain what I’m getting wrong on this level? I only ever send back one friend. Apologies if I’m missing something really obvious, I’m very new to JS. Thanks for the help!
// You can’t reach your friends to defend them!
// Tell them to go home where the archer can help.
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 = hero.findNearestFriend();
if (leastHealth > friend.health && friend.type != “archer”) {
weakestFriend = friend;
leastHealth = friend.health;
}
friendIndex++;
// Tell the weakest friends to go home first.
if (weakestFriend) {
hero.say("Hey " + weakestFriend.id + “, go home!”);
}
}
}
Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
Hey @merryn as @Anonym mentioned welcome to CodeCombat Discourse! Checkout the rules before continuing and have a great time! Now for your code please format it correctly. It helps us to be able to see exactly what is wrong with it without having to figure it out line by line.
`// You can't reach your friends to defend them!
// Tell them to go home where the archer can help.
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 = hero.findNearestFriend();
if (leastHealth > friend.health && friend.type != "archer") {
weakestFriend = friend;
leastHealth = friend.health;
}
friendIndex++;
// Tell the weakest friends to go home first.
if (weakestFriend) {
hero.say("Hey " + weakestFriend.id + ", go home!");
}
}
}
`
1.You should use for loop instead of while loop, to iterate through friends
2.Your if condition is wrong.You must check if friend health is SMALLER than leastHealth.You don’t need the check of friend.type
3.The check of weakestFriend must be outside of for loop
Thanks! I’ll have a go at a for loop, don’t have much experience using them yet to be honest!
I also don’t quite get how leastHealth would be smaller than friend health as it starts on 9999?
Firstly with for loop.
Here is the example of for loop.
for (var i= 0; i < 5; i++){
console.log(i);
}
So, the usual for loop consists of three parts.
The first part var i = 0 is making the variable with which you will iterate.
The second part i < 5 is making the statement by which the for will execute.
The third part i++ is adding the number to iteration variable.
Currently you will need to use the simpler for loop.
for (var friend in friends) {
//some code
}
With this loop you will iterate through an array friends.