# Restless dead javascript

**URL:** https://discourse.codecombat.com/t/restless-dead-javascript/32139
**Category:** Level Help
**Created:** [February 18, 2022, 1:13pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139 "2022-02-18T13:13:58Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 18, 2022, 1:13pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/1 "2022-02-18T13:13:58Z")

</div>

```python
// This level is supposed to be VERY hard! You may need a great strategy and or gear to complete it!

// Find and defeat the yeti then gather its essence for the ritual.
// You might want to gather the coins the yeti leaves behind, you'll need them to summon an army
// Stand at the summoning stone (red x) to begin summoning
// Now you just have to survive the undead hoard
function attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.move(position);
    }
}

moveTo({'x': 56, 'y': 12});

while(true) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        attack(enemy);
    }
    collect();
}

moveTo({'x': 19, 'y': 39});
while (enemy.health > 0) {
    var enemy = hero.findNearestEnemy();
    attack(enemy);
}
collect();
moveTo({'x': 19, 'y': 40});
while(true) {
    summon();
    command();
}

```

the hero is trying to reach the coin, he can’t reach and causing a timeout, please how to fix it?

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 18, 2022, 4:35pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/2 "2022-02-18T16:35:48Z")

</div>

> [@Haris](#):
>
> `hero.move(position);`

try adjusting this to `hero.moveXY(position.x, position.y)`

Also, which hero are you using?

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 18, 2022, 4:42pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/3 "2022-02-18T16:42:39Z")

</div>

im using hattori 20 character

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 3:39pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/4 "2022-02-19T15:39:23Z")

</div>

that not working here new code

```python
// This level is supposed to be VERY hard! You may need a great strategy and or gear to complete it!

// Find and defeat the yeti then gather its essence for the ritual.
// You might want to gather the coins the yeti leaves behind, you'll need them to summon an army
// Stand at the summoning stone (red x) to begin summoning
// Now you just have to survive the undead hoard
function attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveTo(position);
    }
}

var enemies = hero.findEnemies();
for (var i = 0; i < enemies.length; i++) {
    var enemy = enemies[i];
    while (enemy.health > 0) {
        attack(enemy);
    }
}

collect();
hero.moveXY(19, 40);
while(true) {
    summon();
    command();
}

```

hero just ignore the yeti and get killed.

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 3:59pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/5 "2022-02-19T15:59:53Z")

</div>

> [@Haris](#):
>
> ```python
> var enemies = hero.findEnemies();
> for (var i = 0; i < enemies.length; i++) {
> var enemy = enemies[i];
> while (enemy.health > 0) {
> attack(enemy);
> }
> }
> 
> ```

you didn’t put this in the while true loop. this would only run once in the beginning and that would be it. you should also put the collect function in the loop for it to constantly search for the gold

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 4:39pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/6 "2022-02-19T16:39:55Z")

</div>

> [@Haris](#):
>
> ```python
> while (enemy.health > 0) {
> attack(enemy);
> }
> 
> ```

but i already put this as loop as long the enemy health \> 0  
hero will keep attacking.

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 4:55pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/7 "2022-02-19T16:55:27Z")

</div>

yes but the for loop runs once, once the level starts, it checks for enemies, which returns 0 indexes, so no enemy, so it doesn’t attack, and then the for loop finishes

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 4:56pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/8 "2022-02-19T16:56:32Z")

</div>

it can’t cause infinite loops isn’t it?

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 5:17pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/9 "2022-02-19T17:17:04Z")

</div>

why would it? wdym?(20chars)

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:18pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/10 "2022-02-19T17:18:16Z")

</div>

cuz it has 2 loop 20 char

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 5:18pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/11 "2022-02-19T17:18:34Z")

</div>

that’s fine (20 chars)

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:19pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/12 "2022-02-19T17:19:19Z")

</div>

ok that works too i already try but hero stop at the yeti place

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:20pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/13 "2022-02-19T17:20:36Z")

</div>

and not collecting coins

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 5:20pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/14 "2022-02-19T17:20:49Z")

</div>

Code please? (20chars)

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:21pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/15 "2022-02-19T17:21:13Z")

</div>

```python
// This level is supposed to be VERY hard! You may need a great strategy and or gear to complete it!

// Find and defeat the yeti then gather its essence for the ritual.
// You might want to gather the coins the yeti leaves behind, you'll need them to summon an army
// Stand at the summoning stone (red x) to begin summoning
// Now you just have to survive the undead hoard
function attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveTo(position);
    }
}

hero.moveXY(57, 23);
while(true) {
    var enemies = hero.findEnemies();
    for (var i = 0; i < enemies.length; i++) {
        var enemy = enemies[i];
        while (enemy.health > 0) {
            attack(enemy);
        }
    }
    collect();
}

hero.moveXY(19, 40);
while(true) {
    summon();
    command();
}

```

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 5:24pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/16 "2022-02-19T17:24:40Z")

</div>

when i tried running the code, it arised the error of this line

> [@Haris](#):
>
> `hero.moveTo(position);`

make it as i told you, `hero.moveXY(position.pos.x, position.pos.y)`  
also, put everything inside the function in an if statement to check if the item exists

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:25pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/17 "2022-02-19T17:25:47Z")

</div>

wait bruh i forgot to change that

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:27pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/18 "2022-02-19T17:27:13Z")

</div>

work with no error but still hero still stuck at the place

---

<div class="post-metadata">

### Author: ![Haris](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@Haris](https://discourse.codecombat.com/u/Haris)
#### Post date: [February 19, 2022, 5:28pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/19 "2022-02-19T17:28:00Z")

</div>

```python
// This level is supposed to be VERY hard! You may need a great strategy and or gear to complete it!

// Find and defeat the yeti then gather its essence for the ritual.
// You might want to gather the coins the yeti leaves behind, you'll need them to summon an army
// Stand at the summoning stone (red x) to begin summoning
// Now you just have to survive the undead hoard
function attack(enemy) {
    if (hero.isReady("bash")) {
        hero.bash(enemy);
    }
    if (hero.canCast("chain-lightning", enemy)) {
        hero.cast("chain-lightning", enemy);
    }
    else {
        hero.attack(enemy);
    }
}

function collect() {
    var item = hero.findNearestItem();
    if (item) {
        moveTo(item.pos);
    }
}

function summon() {
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    }
}

function command() {
    var enemy = hero.findNearestEnemy();
    var friends = hero.findFriends();
    for (var i = 0; i < friends.length; i ++) {
        var friend = friends[i];
        if (enemy) {
            hero.command(friend, "defend", hero);
        }
    }
}

function moveTo(position) {
    if (hero.isReady("jump")) {
        hero.jumpTo(position);
    }
    else {
        hero.moveXY(position.x, position.y);
    }
}

hero.moveXY(57, 23);
while(true) {
    var enemies = hero.findEnemies();
    for (var i = 0; i < enemies.length; i++) {
        var enemy = enemies[i];
        while (enemy.health > 0) {
            attack(enemy);
        }
    }
    collect();
}

hero.moveXY(19, 40);
while(true) {
    summon();
    command();
}

```

---

<div class="post-metadata">

### Author: ![Aya](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aya/32/42680_2.png) [@Aya](https://discourse.codecombat.com/u/Aya)
#### Post date: [February 19, 2022, 5:29pm UTC](https://discourse.codecombat.com/t/restless-dead-javascript/32139/20 "2022-02-19T17:29:50Z")

</div>

I recommend you use flags for this

Also, no need to make 2 while true loops, just put them in one

[Next page](https://discourse.codecombat.com/t/restless-dead-javascript/32139.md?page=2)
