Mad Maxer: Redemption JS

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!”);
}
}
}

This is a screenshot, may be easier to read the code!

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!

1 Like

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.

Thanks!
-@Luke10

2 Likes

Sorry, I was offline to help

1 Like
`// 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 Like

Hope that’s better. Sorry for the mistake there!

2 Likes

ok, could you please send me the link to the level?

1 Like

CodeCombat - Coding games to learn Python and JavaScript?

1 Like

Thanks, work in progress…

1 Like

CodeCombat - Coding games to learn Python and JavaScript

1 Like

So, I detect three problems:

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

Hope that helps!
(awful 20 chars)

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.

Secondly with leastHealth.
Sorry, I will edit my post again.
Look at it.

Here, to make you get to it faster.

Thank you! Really appreciate it

Did you beat this level?

haha, not yet. Going to try out your suggestions now!