Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Thunderhooves Help! (Javascript)

I’ve pretty much tried everything I can. Please take a look at my code:

while(true) {
    var yak = hero.findNearestEnemy();
    if (yak) {
        if (yak.pos.y > hero.pos.y) {
            hero.buildXY("fence", yak.pos.x , yak.pos.y - 10);
        }
    } else {
            hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
    } else {
        hero.moveXY(hero.pos.x + 10, hero.pos.y
    }
}

Oh, how much i hate this way to format js …

while(true) {
    var yak = hero.findNearestEnemy();
    if (yak) {
        if (yak.pos.y > hero.pos.y) {
            hero.buildXY("fence", yak.pos.x , yak.pos.y - 10);
        } else {
            hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
        } // if yak is not at top it's at bottom, then close if (yak) logic with another }
     } else {  // no yak - go to the target
        hero.moveXY(hero.pos.x + 10, hero.pos.y  // you missed   );
    }
}
 // it can be formatted this way:
while(true) {
    var yak = hero.findYak();
    if (yak) {
        if (yak_is_top) 
            hero.buildBottom(); // if you have only one action after the condition 
        else 
            hero.buildTop();      // you can miss {}
    }
    else
        hero.moveToTarget();  // but this is not the right way to type js
}

rip. i kinda don’t see what I have to do. I closed the hero.moveXY(hero.pos.x + 10, hero.pos.y with the ); but… same message.

EXPECTED AN IDENTIFIER AND INSTEAD SAW ‘ELSE’

you must have this:

hero.moveXY(hero.pos.x + 10, hero.pos.y);

and the partial picture of all {} is this:
изображение :slight_smile:

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Hold up - You can nest else statements in if statements?

And I think its better if I show you a screenshot of what I did so far (following your advice :D)

You want to check if there is a yak. If there is a yak, then check where its position is. If it’s y is greater than your hero’s y, then build a fence 10m below the yak. If the yak’s y is less than your hero’s y position, then build a fence 10m above the yak. Else, there is no yak, move right 10m.

I’m really confused :thinking:

I’m pretty sure that’s what I did.

Can you try to explain this in more depth because I did this over a dozen times and I’m exhausted-

I know python so you would need to format it like this in python:

while True:
    yak = hero.findNearestEnemy()
    if yak:
        if yak.pos.y > hero.pos.y:
             #Build a fence below
        if yak.pos.y < hero.pos.y:
             #Build a fence above
    else: # There is no yak
        #Move right

Not 100% but I think JS would be:

while (true) {
    var yak = hero.findNearestEnemy()
    if (yak) {
        if (yak.pos.y > hero.pos.y) {
            //Build fence below
        }
        if (yak.pos.y < hero.pos.y) {
            //Build fence above
        }
    }
    else {
         //Move Right
    }
}

Just replace the comments with build and move statements.

I took a look at all the replies that tried to help me but I still can’t seem to figure it out… ;(

Could you show your current code again?

I don’t do js, but would it do anything if you did hero.moveXY(hero.pos.x, hero.pos.y + 10

1 Like

But that’s more python so it may do nothing.

That’s what I did but it would just say the same message…

1 Like

My code:

while (true) {
    var yak = hero.findNearestEnemy();
    if (yak) {
        if (yak.pos.y > hero.pos.y) {
            hero.buildXY("fence", yak.pos.x, yak.pos.y - 10);
        }
    } else {
        hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
    }
    {
        hero.moveXY(hero.pos.x + 10, hero.pos.y);
    }
}

In that case, you’ll need more experienced coders. (Who do js

why are you building a fence on the yak’s position if it doesn’t exist?

2 Likes

So how do I fix this problem? ;(

@RenegadePenguin what do you mean by this??

I checked IF the yak existed then I think I did everything right after that…

I’ve tried so many ways to beat this, but I can’t :frowning:

I think you need to check the positioning of your else statement.