[SOLVED] .health creates an error - Sarven Desert: Fair Battle

Not sure if I’m doing something stupid or if this is a bug, but it’s driving me crazy!

I keep getting an error for using .health on an enemy although I’m told by the level’s instructions to do so.

Here’s my function:

function sumHealth(units) {
    var totalHealth = 0;
    var unitsIndex = 0; 
   
    while(true) {
        var unit = units[unitsIndex];   
        if (unitsIndex <= units.length){
            totalHealth += unit.health; 
        }
            unitsIndex+= 1; 
        }   
        return totalHealth;
    }

Here’s a photo of the error:

hi @Kementari, but you want to add the current unit in the array to the totalHealth right? i’ve also been stuck on this level for a long time but i gave up a bit later.

1 Like

Oh, oops (lol). What about using a for loop?

1 Like

yeah i think that’s the most sensible idea

@codewith_haley, do you know for loops?

For loops are something like this:

for ( //code ) {
    // code
}

You could try saying for unit in units

2 Likes

Vaguely - I zoomed through Sarven Desert and was having trouble with Cloudrip Mountain, so I’m working through Sarven Desert more thoroughly.

I’ll try a for loop, but I thinks that’s just a compact version of what i’ve already done.

2 Likes

if you need any help with for loops you can ask me

1 Like

Can you send me your current code so I can help you debug the code?

Dang, that worked lol.

1 Like

So you completed the level or you need any more help?

can you please remove it @codewith_haley ? we don’t want to have working code in the forum.

1 Like

It’s been completed. Thanks for the help y’all!

2 Likes

and i’m also doing the level myself so …

2 Likes

Yes. On the forum we do not show good code because other people could use it to cheat at this level.

1 Like

Noted, this is my first time using the forum and I probably should have been using it early on :sweat_smile:

1 Like

well then, welcome to the forum :partying_face:!

2 Likes

i beat the level myself too (not by looking at the code though)

2 Likes

No problem @codewith_haley! But try to make it so it will not happen again, ok?

1 Like

I also need help, the duck doesn’t like that I am using unit.health instead of hero.health

def sumHealth(units):
    totalHealth = 0;
    unitsIndex = 0; 
    while True:
        unit = units[unitsIndex] 
        if (unitsIndex <= units.length):
            totalHealth += unit.health
            
            unitsIndex+= 1
        
        return totalHealth;

while True:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    if sumHealth(friends) >= sumHealth(enemies):
        hero.say("Attack!")
    else:
        hero.say("Wait.")