Why wouldn't this code work?


#1

I can’t understand why this code wouldn’t work. It says that it expects an identifier instead of the last “else”.
Seems to me if in the first ‘if(enemy)’ statement was not true, that it would skip directly to the last else statement and move the hero back to location 40, 34.

    var enemy = hero.findNearestEnemy();
   
    if(enemy) {
        var dist = hero.distanceTo(enemy); 
        
        if(dist < 5) {
            hero.attack(enemy);
        }
        else {
            hero.shield()
        }
        
    
    else {
        hero.moveXY(40, 34);
    }
}```

#2

Can you format all of your code chances are its a bracket problem. Its easier to see the brackets when it all formatted.

From the looks of it your while loop doesnt have an ending bracket. You also need a ; at the end of hero.shield()


#3

I cannot for I already backed out of that game.


#4

and I thought the very last bracket closed out the ‘while true’ statement


#5

I see what you are saying though. This is my biggest problem is keeping up with all these brackets. Looks like I didn’t have one to close out the ‘if enemy’ statement. Are lack of these brackets be why it was giving me the original error message?


#6

Yes it possible but you also have the missing ;


#7

You can edit your post and put in 3 backstick before and after the code still or highlight everything and click the button </> which is in the top bar of the text editior.


#8

Thanks. Just knowing how to format my posts will help a bunch.