# \[Help\] Basin Stampede (JavaScript)

**URL:** <https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249>\
**Category:** Uncategorized\
**Created:** [January 10, 2017, 9:30pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249 "2017-01-10T21:30:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 10, 2017, 9:30pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/1 "2017-01-10T21:30:11Z")

</div>

Objective

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/3/37602c0aa604a4127a9ac6eeeca4e2c4e007e338.png)

```python
// Keep moving right, but adjust up and down as you go.

while(true) {
    var enemy = hero.findNearestEnemy(); //Find the Nearest Enemy
    var xPos = hero.pos.x + 5; //Set the heroes postion to his X + 5 the way he will walk to the right?
    var yPos = 17; //Set yPosition to 17
    var subtractYPos = yPos - 3; //Set new y position to current Y position - 3
    var addYPos = yPos + 3; //Set new y position to current Y position + 3
    if(enemy) {
        // Adjust y up or down to get away from yaks.
        if(enemy.pos.y > hero.pos.y) { //If the enemy position is > hero position
            // If the Yak is above you, subtract 3 from yPos.
            hero.moveXY(xPos, subtractYPos); //Hero will move along same X position but the new Y Position in this cas current Y - 3
        } else if (enemy.pos.y < hero.pos.y) { // Else hero will
            // If the Yak is below you, add 3 to yPos.
            hero.moveXY(xPos, addYPos); //Hero will move along the same X position but to the current Y postion plus 3
        }
    }
    hero.moveXY(xPos, yPos);
}

```

What is happening when I run the code:  
[![](https://media.giphy.com/media/xTgeJ7fOWqj2gaSfbG/giphy.gif "Animated GIF - Find & Share on GIPHY") ](https://media.giphy.com/media/xTgeJ7fOWqj2gaSfbG/giphy.gif)

I don’t understand what I am doing wrong at all I thought my code makes perfect sense. So any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [January 10, 2017, 9:56pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/2 "2017-01-10T21:56:33Z")

</div>

it looks like the character is responding the first and second yak and then before he returns to the center position (y 17).he runs into the third yak. Try limiting the yaks he responds to by limiting the distance to which he will take action. I did it by adding an if statement after if enemy. If hero.distanceTo(enemy) \< 5:

This way he ignores yaks that are too far away to be of any consequence. Everything else you have looks right although where you have a 5, I put a 10 to move him along faster.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 10, 2017, 10:13pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/3 "2017-01-10T22:13:18Z")

</div>

I got it to work by just deleting the entire code and starting from scratch I made this new code and it worked

```python
while(true) {
    var enemy = hero.findNearestEnemy();
    var x = hero.pos.x + 5;
    var y = 17;
    if(enemy && enemy.pos.y > hero.pos.y) {
        hero.moveXY(x, y - 3);
    } else if (enemy && enemy.pos.y < hero.pos.x) {
        hero.moveXY(x, y + 3);
    } else {
        hero.moveXY(x, y);
    }
}

```

But can someone explain why it doesn’t work if the code is written this way? Isn’t it the same exact thing I just have it in a different order? Or does the order really matter.

```python

while(true) {
    var enemy = hero.findNearestEnemy();
    var x = hero.pos.x + 5;
    var y = 17;
    if(enemy && enemy.pos.y < hero.pos.y) {
        hero.moveXY(x, y + 3);
    } else if (enemy && enemy.pos.y > hero.pos.x) {
        hero.moveXY(x, y - 3);
    } else {
        hero.moveXY(x, y);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [January 10, 2017, 10:21pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/4 "2017-01-10T22:21:11Z")

</div>

Luck. `else if(enemy && enemy.pos.y > hero.pos.x)` You’re comparing `pos.y` to `pos.x`

Note this level you’re meant to do:

```python
yPos += 3;
// Other if statement
yPos -= 3;

```

Not move twice in the same while-true loop, which is why your original code was failing.

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 10, 2017, 10:33pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/5 "2017-01-10T22:33:17Z")

</div>

Wow! I didn’t even realize I made that mistake, but changing it to **+=** and **-=** did fix it for me. But why doesn’t y + 3 or y - 3 work. Isn’t the same thing happening? Or does this just add + 3 then I have to go back to my default location which is 17 because Y was never set to the new value after 3 was added? So then += will take the current value of Y add 3 to and then change the current y to the new y making it 20 instead of 17? Then depending on where a yak is coming from I will either add or subtract from the new current Y which would be 20?

---

<div class="post-metadata">

**Author:** ![BobBobson412](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bobbobson412/32/6871_2.png) [@BobBobson412](https://discourse.codecombat.com/u/BobBobson412)\
**Post date:** [January 11, 2017, 1:56pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/6 "2017-01-11T13:56:36Z")

</div>

y + 3 or y - 3 doesn’t work just because of the language you are using. If you were using Java Script you would have to use += or -= in a math situation unless you used or created a different language it is just how Java Script works.

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 11, 2017, 3:36pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/7 "2017-01-11T15:36:42Z")

</div>

y + 3 or y - 3 does work. It doesnt work the way he has it because hes not reassigning the value to y.  
`hero.moveXY(x, y - 3) #This doesnt change the value of y`  
`y += 3 #Changes the value of y.`

Both statements work but they are doing 2 differnt operations.

```python
y = 17
hero.say(y - 3) #14
hero.say(y) #17

y= 17
hero.say(y-=3) #14 #Note this statement does not work I just wanted to show what its was essentialy doing.
hero.say(y)#14

```

---

<div class="post-metadata">

**Author:** ![Cydo](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@Cydo](https://discourse.codecombat.com/u/Cydo)\
**Post date:** [January 11, 2017, 3:40pm UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/8 "2017-01-11T15:40:17Z")

</div>

Thanks guys I think I understand it now.

---

<div class="post-metadata">

**Author:** ![Nicole\_Deni](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nicole_deni/32/6446_2.png) [@Nicole\_Deni](https://discourse.codecombat.com/u/Nicole_Deni)\
**Post date:** [June 17, 2017, 1:39am UTC](https://discourse.codecombat.com/t/help-basin-stampede-javascript/10249/9 "2017-06-17T01:39:52Z")

</div>

thank you, this code really help! I still dont understand why it has to be soooo picky! 😀
