Javascript: Wild Horses Help [Solved]

while (true) {
    // How do you find the nearest friendly unit?
    // horse = ?
    var horse = hero.findFriends();
    if (horse) {
        var x1 = horse.pos.x - 7;
        var x2 = horse.pos.x + 7;
        if (x1 >= 1) {
            // Move to the horse's y position but use x1 for the x position.
            hero.moveXY(x1, horse.pos.y);
        } else if (x2 <= 79) {
            // Move to the horse's y position but use x2 for the x position.
            hero.moveXY(x2, horse.pos.y);
        }
        var distance = hero.distanceTo(horse);
        if (distance <= 10) {
            hero.say("Whoa");
            // Move to the red x to return the horse to the farm.
            hero.moveXY(26, 54);
            // Move back out into the pasture to begin looking for the next horse.
            hero.moveXY(67, 22);
        }
    }
}

Here is my code, on this line:

        var x1 = horse.pos.x - 7;

it gives error: “horse is not defined”.
Thanks

Ill start up CoCo and then Ill help you ok?

1 Like

ok (20 character and 20 chars.)

1 Like

I need to still complete wild horses on javascript, but I think i can do it quick

1 Like

That should be:

var horse = hero.findNearest(hero.findFriends())

You need to find the one unit, not an array.

1 Like

it should work now, I just tried it

1 Like

or you could do

var horse= hero.findNearestFriend();
1 Like

I added it but it says “ran out of time” and all but one end up behind the fence.
Thanks

1 Like

Keep submitting, it should work.

1 Like

ok I think I did this level in a different way…

1 Like

yeah you should keep submitting it…

1 Like

I tried that but nothing changed.

1 Like

Show us your current code

1 Like

Maybe you are moving to the wrong spots. Try a different position in the pasture.

1 Like
while (true) {
    var horse = hero.findNearest(hero.findFriends());
    if (horse) {
        var x1 = horse.pos.x - 7;
        var x2 = horse.pos.x + 7;
        if (x1 >= 1) {
            hero.moveXY(x1, horse.pos.y);
        } else if (x2 <= 79) {
            hero.moveXY(x2, horse.pos.y);
        }
        var distance = hero.distanceTo(horse);
        if (distance <= 10) {
            hero.say("Whoa");
            hero.moveXY(26, 54);
            hero.moveXY(67, 22);
        }
    }
}

Here is my current code. @JoPro_8000

1 Like

Well, Idk what to do now, its literally my exact code and mine worked

1 Like

Go directly to the farmhouse. Not two moveXYs

1 Like

Here try moving to (39,45). Then keep submitting.

nevermind for my post.

you’re using soft leather boots