# Sarven Treasure Javascript

**URL:** https://discourse.codecombat.com/t/sarven-treasure-javascript/21933
**Category:** Uncategorized
**Created:** [May 31, 2020, 1:35pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933 "2020-05-31T13:35:56Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Poltan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/poltan/32/11958_2.png) [@Poltan](https://discourse.codecombat.com/u/Poltan)
#### Post date: [May 31, 2020, 1:35pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/1 "2020-05-31T13:35:56Z")

</div>

Hello! Can anybody, pls, help me with this code (I believe this is an easy issue for you)?

The problem is that my hero doesn’t pick up gold after killing ogres and I don’t understand why…

```python
while (true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;
    var coins = hero.findNearestItem();
    var coinIndex = 0;
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy) {
            hero.attack(enemy);
        }
        enemyIndex += 1;
    } 
    if (!enemies && coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        coinIndex += 1;
    }
}

```

---

<div class="post-metadata">

### Author: ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)
#### Post date: [May 31, 2020, 1:37pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/2 "2020-05-31T13:37:30Z")

</div>

> [@Poltan](#):
>
> `!enemies && `

Try to remove the text it shows

---

<div class="post-metadata">

### Author: ![Poltan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/poltan/32/11958_2.png) [@Poltan](https://discourse.codecombat.com/u/Poltan)
#### Post date: [May 31, 2020, 1:39pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/3 "2020-05-31T13:39:17Z")

</div>

Tried with `!enemies &&` and without this line.

Doesn’t work either way.

The hero just stands still in the middle.

---

<div class="post-metadata">

### Author: ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)
#### Post date: [May 31, 2020, 1:40pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/4 "2020-05-31T13:40:00Z")

</div>

What is your code without that?

---

<div class="post-metadata">

### Author: ![Poltan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/poltan/32/11958_2.png) [@Poltan](https://discourse.codecombat.com/u/Poltan)
#### Post date: [May 31, 2020, 1:46pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/5 "2020-05-31T13:46:40Z")

</div>

```python
while (true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;
    var coins = hero.findNearestItem();
    var coinIndex = 0;
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy) {
            hero.attack(enemy);
        }
        enemyIndex += 1;
    } 
    if (coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        coinIndex += 1;
    }
}

```

---

<div class="post-metadata">

### Author: ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)
#### Post date: [May 31, 2020, 1:50pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/6 "2020-05-31T13:50:34Z")

</div>

Take a closer look at this:

```javascript
    if (!enemies && coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
        coinIndex += 1;
    }

```

Because this is an ‘if block’, it runs only once; because it is a member of the ‘while true’ loop, it will run once per iteration. However, you also reset ‘coinIndex’ to 0…coinIndex is always going to === 0

---

<div class="post-metadata">

### Author: ![Jeremy\_Yu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jeremy_yu/32/12445_2.png) [@Jeremy\_Yu](https://discourse.codecombat.com/u/Jeremy_Yu)
#### Post date: [May 31, 2020, 1:56pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/7 "2020-05-31T13:56:41Z")

</div>

I am having problems too but i am on Sarven treasure 4

---

<div class="post-metadata">

### Author: ![Jeremy\_Yu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jeremy_yu/32/12445_2.png) [@Jeremy\_Yu](https://discourse.codecombat.com/u/Jeremy_Yu)
#### Post date: [May 31, 2020, 2:00pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/8 "2020-05-31T14:00:22Z")

</div>

```python
while True:
    enemys = hero.findEnemies()
    items = hero.findItems()
    item = hero.findNearest(items)
    enemy = hero.findNearest(enemys)
    if (enemy and hero.distanceTo(enemy) < 15):
        hero.attack(enemy)
    elif (item):
        if (hero.isReady('jump')):
            hero.jumpTo(item.pos)
        else:
            hero.move(item.pos)
        

```

---

<div class="post-metadata">

### Author: ![Jeremy\_Yu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jeremy_yu/32/12445_2.png) [@Jeremy\_Yu](https://discourse.codecombat.com/u/Jeremy_Yu)
#### Post date: [May 31, 2020, 2:02pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/9 "2020-05-31T14:02:49Z")

</div>

This is my js code:

```python
while (true) {
    enemys = hero.findEnemies();
    items = hero.findItems();
    item = hero.findNearest(items);
    enemy = hero.findNearest(enemys);
    if (enemy and hero.distanceTo(enemy) < 15) {
        hero.attack(enemy)
   } else (item) {
        if (hero.isReady("jump")) {
            hero.jumpTo(item.pos)
         } else {
            hero.move(item.pos)}
       }
}
    

```

---

<div class="post-metadata">

### Author: ![Poltan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/poltan/32/11958_2.png) [@Poltan](https://discourse.codecombat.com/u/Poltan)
#### Post date: [May 31, 2020, 2:20pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/10 "2020-05-31T14:20:47Z")

</div>

turned it into while-loop

```python
while (!enemies && coinIndex < coins.length) {
        var coin = coins[coinIndex];
        if (coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }

```

And nothing happened.

I got a feeling that there’s an infinite loop with killing ogres and my character’s not breaking from it.

---

<div class="post-metadata">

### Author: ![Jeremy\_Yu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jeremy_yu/32/12445_2.png) [@Jeremy\_Yu](https://discourse.codecombat.com/u/Jeremy_Yu)
#### Post date: [May 31, 2020, 2:33pm UTC](https://discourse.codecombat.com/t/sarven-treasure-javascript/21933/11 "2020-05-31T14:33:55Z")

</div>

if there is an infinite loop the level will alert you
