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

[SOLVED] Help with Thunderhooves! (javascript)

Hi there!

I’ve been having some difficulties with the thunderhooves level. I just can’t figure out what I’m doing wrong! I’ve been getting a weird error saying “Unreachable ‘while’ after ‘return’.”

…help?

 loop {
    var yak = this.findNearestEnemy();
     if (yak) {
         // A yak is above you if it's y is greater than your y.
         // If the yak is above you, build a fence 10m below it.
         if(yak.pos.y < this.pos.y){
             var y = yak.pos.y + 10;
             var x = yak.pos.x;       
             this.buildXY("fence", x, y);            
          }
         if(yak.pos.y > this.pos.y){
             y = yak.pos.y - 10;
            x = yak.pos.x;
             this.buildXY("fence", x, y);
         }         
     }
    else {       
         var xx = this.pos.x + 10;
         var yy = this.pos.y;
         this.moveXY(xx, yy);
 }

 }
1 Like

Do you have a line like this at the top of your code?

return;  // Commented out to stop infinite loop

If so, it was inserted by the level-is-not-loading popup. You can delete that line.

I can’t believe I didn’t see that before! Thank you :slight_smile:

I’m also having problems with my code. When I run it, Anya doesn’t move at all…

loop {
var yak = this.findNearestEnemy();
if (yak) {
    var yakY = this.yak.pos.y;
    var yakX = this.yak.pos.x;
    if (yakY > this.pos.y) {
        this.buildXY("fence", yakX, yakY - 10);
    }
    if (yakY < this.pos.y) {
        this.buildXY("fence", yakX, yakY + 10);
    } else {
        var X = this.pos.x + 10;
        var Y = this.pos.y;
        this.moveXY(X, Y);
    }
}

}

Your problem is this.yak.pos.y and this.yak.pos.x. They should just be yak.pos.y and yak.pos.x.

I can’t believeI didn’t see that…Thank you!

Hey, I am wondering what I am doing wrong, my hero is not moving at all, but I don’t get any " error" messages :confused: Thanks so much to anyone replying!!!

Edit: Got the error^^

loop {
    var yak = this.findNearest(this.findEnemies());
    if (yak) {
     
    var yakX = yak.pos.x;
    var yakY = yak.pos.y;
    
       if (yakY<this.pos.y) {
           this.buildXY("fence",yakX,yakY+10);
       }

     if (yakY>this.pos.y) {
         this.buildXY("fence", yakX, yakY-10); 
     }
         
 else {  
    

        var newX = this.pos.x+10;
        var newY= this.pos.y;
        this.moveXY(newX, newY);     
    }
}
}

this code worked. I wasn’t able to figure out what I was doing wrong, but yours worked perfectly. :smile:

Please help… I can’t get my char to put down more than one fence. Here’s my code

// Get to the oasis,
// fencing off paths with randomized yaks on them as you go.
loop {
    var yak = this.findNearestEnemy();
    if (yak) {
        var distance = this.distanceTo(yak);
        if (distance < yak.pos.y) {
            this.buildXY("fence", yak.pos.x , yak.pos.y - 10);
        }
         if (distance > yak.pos.y) {
            this.buildXY("fence", yak.pos.x, yak.pos.y + 10);
        }


        // A yak is above you if yak.pos.y is greater than your pos.y.
        
            // If the yak is above you, build a fence 10m below it.
        
            // If the yak is below you, build a fence 10m above it.
        
    } else {
    var newX = this.pos.x + 10;
    var newY = this.pos.y;
        this.moveXY(newX, newY);

        // Move right 10m towards the oasis.
        
    }
}

This is wrong:

var distance = this.distanceTo(yak);
        if (distance < yak.pos.y) {

The comment hint for the level translates into:

// A yak is above you if yak.pos.y is greater than your pos.y
if(this.pos.y < yak.pos.y)

this.pos is your position. Generally, given an enemy, friend, item or flag, thingyName.pos is its position and thingyName.pos.x is the X coordinate

Yeah I figured it out about 5 seconds after I posted this… Thanks for the advice though!

@PokasLittleOne No one posts finished/working code here. We just post our bugged/not working code and then people offer suggestive hints to repair it in order to make the person think and learn. If we all posted our working/completed code, then no one would be forced to use their brain and learn anything. It is not helpful to just give people the answers. Please delete your post.

All right that’s fine. It’s just that I know how difficult it can be to try and figure these things out, I know from personal experience. Just thought that I’d help give a reference code to help them see what they are doing wrong and try to fix it. But I see how that’s defeating the point, I won’t do it again.

Hi everyone, i’m having troubles with this level too, my hero build first fence below the yak coming from top, then moving to the Oasis, but then he tries to build a fence behind the yak coming from below and I can’t figure ou why. I think it is because my hero consider the first yak as the nearest enemy when he spot the second one ? Here is my code, if you can give me an hint.

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

Here is my code:

// Move right, to the oasis.
// Build a "fence" above or below when you see a yak.

while(true) {
    var yak = hero.findNearestEnemy();
    if (yak) {
        // If yak.pos.y is greater than hero.pos.y
        if (yak.pos.y > hero.pos.y) {
           hero.buildXY("fence", 36, 30);
           // buildXY a "fence" 10m below the yak.
        hero.buildXY("fence", yak.pos.x, yak.pos.y - 10);
        } else {
           // else:
           // buildXY a "fence" 10m above the yak. 
         hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
    } else {
        // moveXY right 10m towards the oasis.
        hero.moveXY(hero.pos.x + 10, hero.pos.y);
    }
}



I did what the code said but it said "Expected an identifier and instead 
saw 'else' ". Why and how do I fix it?

Howdy and welcome to the forum!

Please be sure to post your code properly formatted. You can learn how here: [Essentials] How To Post/Format Your Code Correctly