# Sand Snakes Javascript, need help

**URL:** <https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652>\
**Category:** Uncategorized\
**Created:** [August 17, 2018, 5:27pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652 "2018-08-17T17:27:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrendGroen](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@BrendGroen](https://discourse.codecombat.com/u/BrendGroen)\
**Post date:** [August 17, 2018, 5:27pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/1 "2018-08-17T17:27:29Z")

</div>

This is my code:

```python
// This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
// This canyon seems to interfere with your findNearest glasses!
// You'll need to find the nearest coins on your own.
while (true) {
    var coins = hero.findItems();
    var coinIndex = 0;
    var nearest = null;
    var nearestDistance = 9999;
    // Loop through all the coins to find the nearest one.
    while (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        coinIndex++;
        var distance = hero.distanceTo(coin);
        // If this coin's distance is less than the nearestDistance
        if (distance < nearestDistance) {
            // Set nearest to coin
            var nearest = coin;
            // Set nearestDistance to distance
            var nearestDistance = distance;
        }
        // If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

```

It will take the 1st coin and then move to another coin, but then it hits the mines…

---

<div class="post-metadata">

**Author:** ![Archion](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/archion/32/26234_2.png) [@Archion](https://discourse.codecombat.com/u/Archion)\
**Post date:** [August 17, 2018, 5:29pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/2 "2018-08-17T17:29:53Z")

</div>

That is a glitch. it happened to me 2. Try using flags if it doesn’t work @BrendGroen

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [August 17, 2018, 6:19pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/3 "2018-08-17T18:19:37Z")

</div>

@Archion when I run the level there’s no glitch or anything. I didn’t use flags.

---

<div class="post-metadata">

**Author:** ![BrendGroen](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@BrendGroen](https://discourse.codecombat.com/u/BrendGroen)\
**Post date:** [August 17, 2018, 6:20pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/4 "2018-08-17T18:20:13Z")

</div>

@SuperSmacker when I run the level it runs into a bomb while my code was correct ☹

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [August 17, 2018, 6:20pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/5 "2018-08-17T18:20:49Z")

</div>

What I’m saying is that your code isn’t correct

For one, why’s the “if coin” loop within the while loop?

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [August 17, 2018, 6:23pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/6 "2018-08-17T18:23:06Z")

</div>

@Archion I ran the level just now. It works. I beat it without using flags.

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [August 17, 2018, 6:48pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/7 "2018-08-17T18:48:35Z")

</div>

@maka the default code for sand snakes has a problem.

The “coinIndex++” should be at the end of the nested while loop, not at the beginning of it.

If you move it to the end then follow the instructions, then the level works perfectly.

---

<div class="post-metadata">

**Author:** ![kruikje](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@kruikje](https://discourse.codecombat.com/u/kruikje)\
**Post date:** [May 26, 2023, 10:42am UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/8 "2023-05-26T10:42:05Z")

</div>

Hi, i have the same problem. Nothing is happening and i don’t see any mistakes in my code.  
Anyone an idea of what is wrong.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/5/d/5de5d0cfaa6e51537f352670c9323e1ed8f30b61.png)

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [May 26, 2023, 11:19am UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/9 "2023-05-26T11:19:32Z")

</div>

Hi, welcome to the Discourse!  
In your code, `coinIndex++` is at the end of the if loop, not the while loop. Move it out of the if loop and your code should work fine.

---

<div class="post-metadata">

**Author:** ![kruikje](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@kruikje](https://discourse.codecombat.com/u/kruikje)\
**Post date:** [May 26, 2023, 11:46am UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/10 "2023-05-26T11:46:54Z")

</div>

Thanks for the reply, is it in the right loop?

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/e/8/e8fe2a08f9abca97be399a65e54b05b60117214b.png)  
because it still won’t work. Any other idea what it could be

---

<div class="post-metadata">

**Author:** ![MarmiteOnToast](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marmiteontoast/32/46649_2.png) [@MarmiteOnToast](https://discourse.codecombat.com/u/MarmiteOnToast)\
**Post date:** [May 26, 2023, 5:07pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/11 "2023-05-26T17:07:55Z")

</div>

Yeah, that’s the right loop.  
The last `if` loop in your script is indented too far, but I don’t think that matters in JS…  
Could you please post what gear your hero is using? Maybe the glasses don’t have long enough range

---

<div class="post-metadata">

**Author:** ![kruikje](https://avatars.discourse-cdn.com/v4/letter/k/7cd45c/32.png) [@kruikje](https://discourse.codecombat.com/u/kruikje)\
**Post date:** [May 26, 2023, 5:21pm UTC](https://discourse.codecombat.com/t/sand-snakes-javascript-need-help/15652/12 "2023-05-26T17:21:33Z")

</div>

![Screenshot_2023-05-26-19-20-06-49_40deb401b9ffe8e1df2f1cc5ba480b12](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/b/5/b58dfbdf315a013127361b1d54454f3603ba6895.jpeg)
