Lv:the mighty sand yak

I get a “time error” ending.I just wanna say if i did not complete “Dodge four yaks” task, I must be killed that means that I am not alive.Who can tell me the reason why I am alive, but cannot pass this task?THX.
Here is my js code:

a = 10;
loop {
    // Use "if" to only move when a yak is less than 10m away.
    // Move right by adding to your current X position.
    // Use your Sense Stone to access this.pos.
    if (this.pos.x > 40) {
        a = -10;
    } else if (this.pos.x < 21) {
        a = 10;
    }
    enemy = this.findNearestEnemy();
    if (enemy)
    if (enemy.pos.x > this.pos.x - 5 && 
        enemy.pos.x < this.pos.x + 5) {
        this.moveXY(this.pos.x + a, this.pos.y);
    }
}
loop {
    if (this.distanceTo(this.findNearestEnemy()) < 10 ){
        this.moveXY(this.pos.x+10, this.pos.y);
    }
}

I know this is right, but why the first section code cannot pass.THX.

Not sure what is your problem, but to be sure what is calculated here, I advice you to do:

if ( (enemy.pos.x > (this.pos.x - 5)) && 
        (enemy.pos.x < (this.pos.x + 5)) ) 

it could also be good to add a else to your if for the actions. If you have no action in your loop, it could lead you to strange results. So you can add another move if there is a yak, or to just wait a little bit, add a this.say(“anything”).

my 2 euros.

THX,I just give up to play this game yesterday.But THX.