Backwoods forest level 11, Can not figure it out


#1

// If there is an enemy, attack it.
// Otherwise, attack the chest!

while(true) {
// Use if/else.

        if (enemy) {
        hero.attack(enemy);
        hero.attack(enemy);
        hero.attack(chest);
        }

#2

I’m not sure if you copied all your code.

Can you please surround your code in triple-backticks ( ` ) or put it inside a code block it is properly formatted?


#3

Hi Freddy, that if-statement tells your hero to attack the chest only if there is an enemy first.


#4

I’ve tried every possible way and its not letting me do anything


#5

Have you tried,

if (enemy) {
hero.attack(enemy);
hero.attack(enemy);
}
else {
hero.attack(chest);
}

???


#6

yes, I tried just bout every possible way…just tried it out and is not working


#7

What equipment do you have? And can you post your current code properly formatted (surrounded with triple backticks on top and bottom of the code: ( ` ))


#8

I don’t know if this is what you mean:

while(true) {
    // Use if/else.
if (enemy) {
    hero.attack(enemy);
    hero.attack(enemy);
}
else {
    hero.attack(chest);
}

#9

Close but you want to use ( ` ) instead of ( ’ ).

Anyways, about your code:

enemy is not defined. Look back at previous levels to see how to define an a variable with the nearest enemy.

Similarly chest isn’t defined, but, in this level you need to use "Chest" instead of variable.


#10

made progress but still can’t get to the last ogre in time. This is my new code

    // Use if/else.
    hero.attack("Chest");
    var enemy = this.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }   
    if (!enemy) {
        hero.attack("Chest");
    }
    
    
}

#11

Sometimes it helps to reload the code from scratch by clicking the “Reload” button at the top right corner of the game window.

But, to fix what you have, you want to use if/else instead of if and if not.

Also by attacking the chest at the start of the loop, you let munchkins hit you for longer than you should. This is why I recommend reloading the code so you can see the original sample.


#12

Tried this instead:

// Otherwise, attack the chest!

while(true) {
    // Use if/else.
    var enemy = this.findNearestEnemy();
    if/else (enemy) {
        hero.attack(enemy);
    }
    hero.attack("Chest");
    
}

and still nothing


#13

figured out that it wasn’t my code but the fact that I needed to upgrade my equipment