Backwoods treasure in JS

Hi, I was triyng for few days this level in the the forest. The problem is that in the middle of tha dventure my character does not move at all. I need a solutoin. I had been used this code:

loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    var flag = this.findFlag("green");
    if (item){
         if (enemy){
             if (this.isReady("cleave")){
                 this.cleave(enemy);
             }
             else {
                 this.attack(enemy);
             }
    }     
       else {
        var ItemPos = item.pos;
        var Ix = ItemPos.x;
        var Iy = ItemPos.y;
        this.moveXY(Ix, Iy);
        }
    }
}

Thank you!!

Hi yupiyu,
please spellcheck before submitting, as it vastly improves the readability.


You do nothing with your flag, so it is not needed. You might want to adjust that.

Try to read your code out loud:

If there is an item ...
    and if there is an enemy, engage the enemy.
    else do nothing.
else (==there is no item) collect the item.

As one can see, you only ever attack an enemy when there is an item as well, and you only try to collect an item if there is no item around.

**Try to solve it yourself first!**

You need to reorder the statements:

//NOT COMPLETE CODE!!!
loop {
    var enemy = this.findNearestEnemy();
    var item = this.findNearestItem();
    if (enemy){
        //engage enemy)
    }
    else if (item) {
        //collect item
    }
}

Are you sure?

I see three ‘}’ after the else “get item”

loop {             
    if item{
        if enemy{

makes three

The ‘}’ before the else is deceptively indented.

Otherwise, :smile: spot on!

no item == no action . . . bummer dude. (depending on the glasses can you see far enough??)

There will come a time when you won’t do anything (but fight) since the only coins will be in other sections of the map and you won’t be able to see them (unless you have the see through walls glasses) so you will have to decide to walk out of your current section into another.

OK, thank you to everyone. the problem was that I could see far enough for get the rest of the coin. So what I did?
"if(!item){
move to the center}
"
Thank you so much.

1 Like