SOLVED mind the trap

I am strggling to do this level, Could anyone help asap?

Here’s the level:

https://codecombat.com/play/level/mind-the-trap?
Here’s my code

// If you try to attack a distant enemy, your hero will charge toward it, ignoring all flags.
// You'll need to make sure you only attack enemies who are close to you!

while(true) {
    var flag = hero.findFlag();
    var enemy = hero.findNearestEnemy();
    
    if(flag) {
        // Pick up the flag.
        hero.moveXY(flag.pos.x, flag.pos.y);
        hero.say("I should pick up the flag.");
    } else if(enemy) {
        // Only attack if the enemy distance is < 10 meters
        var distance = hero.distanceTo(enemy);
    if (distance < 50) {
        hero.attack(enemy);
    }
    }
}

Here’s what’s it doing:

1 Like

[Code removed by a moderator: Please don’t post successful solutions as it is counterproductive to the learning process. Great to hear you solved the level, though!]

Hello! I’m glad you solved the level! But please don’t show the working code to others. If someone is simply given the code, they won’t have learned anything, and CodeCombat is solely about learning.

3 Likes

Okay, Won’t do next time :slight_smile: Just first time doing it

1 Like