Wild horses in javascript always ran out of time


#1

I am trying collect all the horses put in the fence. however I always got one reminder, it ran out of time.

How would you successful for this level?

one more thing I dont understand why when they say “hi” and move back to “geez” by added 7 and move back to “hi” back forth over till they see the horse.

What I am trying to do is say “hi” several of time by subtract -7 number till the Conditional Statements become false. If I want to set up Conditional Statements for (x2 <= 79) (this.say(“geez”)) it refused let my hero walk to all the way to right side at appox 77 number then the Conditional Statements will become false back to (x1 >= 1)?


loop {
    // How do you find the nearest friendly unit?
    // horse = ?
     var horse = this.findNearest(this.findFriends());
    if (horse) {
        var x1 = this.pos.x - 7;
        var x2 = this.pos.x + 7;
        var y = this.pos.y;
        // var y1 = this.pos.y - 1;
        if (x1 >= 7) {
            // Move to the horse's y position but use x1 for the x position.
            this.moveXY(x1, y);
            // x1 += 7;
            // this.moveXY(x1, y);
            this.say('hi');
            
        } 
        else if (x2 <= 79) {
            // Move to the horse's y position but use x2 for the x position.
            this.moveXY(x2, y);
            // x2 -= 7;
            // this.moveXY(x2, y);
            this.say('geez');
        }
        var distance = this.distanceTo(horse);
        if (distance <= 15) {
            this.say("Whoa");
            // Move to the red x to return the horse to the farm.
            this.moveXY(27, 54);
            this.say("stay!");
            // Move back out into the pasture to begin looking for the next horse.
            this.moveXY(28, 18);
        }
    }
}



#2

So, the comments say you need to move to the horses y position - but you seem to be moving to your y position in this code?
Also I don’t think you need the hi and geez say commands.

Maybe try those, and see if it helps round up the horses.


#3

Am I supposed to change the var y = this.pos.y;? I dont think so because when see the horse its will automatically change the number toward the red X to return the horse to the farm. I have been struggled with this level. I dont know how to beat the timer. maybe can you help me or feedback with this?

and other reason I want to put the hi and geez because it will help me understand what they do with the code. I will remove the commands when i need.


#4

You said you run out of time, matt is trying to tell you that the extra “say” commands USE time . . . so take them out and maybe you’ll have enough.


#5

Yes, removing the say commands should help. But the comments say you should move to the horses y position, i.e. horse.pos.y, but you are going to this.pos.y. Which I think means you won’t be going to the correct y position on the screen.