# Desert Level Double Cheek - default code causes infinite loop error

**URL:** https://discourse.codecombat.com/t/desert-level-double-cheek-default-code-causes-infinite-loop-error/8926
**Category:** Adventurer
**Created:** [September 10, 2016, 8:12pm UTC](https://discourse.codecombat.com/t/desert-level-double-cheek-default-code-causes-infinite-loop-error/8926 "2016-09-10T20:12:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Harry\_the\_Wanderer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/harry_the_wanderer/32/5369_2.png) [@Harry\_the\_Wanderer](https://discourse.codecombat.com/u/Harry_the_Wanderer)
#### Post date: [September 10, 2016, 8:12pm UTC](https://discourse.codecombat.com/t/desert-level-double-cheek-default-code-causes-infinite-loop-error/8926/1 "2016-09-10T20:12:00Z")

</div>

When I start the level with Javascript code (first time I believe) it said there was an infinite loop?

> **Error Image**
>
> ![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/codecombat/original/2X/b/b9469cdb1a97e8a66606589c19ad9d798935a7cc.png)

Code:

```python
return; //Commented out to stop infinite loop.
// Kill at least 6 ogres on the left side.
// Then, collect at least 30 gold on the right.
// This variable is used for counting ogres.
var defeatedOgres = 0;

// This loop is executed while "defeatedOgres" is less than 6.
while (defeatedOgres < 6) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
        defeatedOgres += 1;
    } else {
        hero.say("Ogres!");
    }
}

// Move to the right part of the map.
hero.moveXY(49, 36);

// This loop is executed while you have less than 30 gold.
while (hero.gold < 30) {
    // Find and collect coins.
    
}

// Move to the exit.
hero.moveXY(76, 32);

```

I am guessing this is from the `while (hero.gold < 30) {` statement line where there is nothing in the loop.

Adding a hero.say() should fix it like:

```python
while (hero.gold < 30) {
    // Find and collect coins.
    hero.say("Get the coins");
}

```

---

<div class="post-metadata">

### Author: ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)
#### Post date: [September 11, 2016, 4:23am UTC](https://discourse.codecombat.com/t/desert-level-double-cheek-default-code-causes-infinite-loop-error/8926/2 "2016-09-11T04:23:41Z")

</div>

Yeah. It’s a problem with `while expression:` and if your hero is fast.

> Adding a hero.say() should fix it like:

It can help, but it’s not a solution, because players can remove it and get the “infinity loop”.

I have an idea how to fix (optimise) it. Will do it today.

---

<div class="post-metadata">

### Author: ![premalatha\_balan](https://avatars.discourse-cdn.com/v4/letter/p/f6c823/32.png) [@premalatha\_balan](https://discourse.codecombat.com/u/premalatha_balan)
#### Post date: [August 5, 2017, 3:49pm UTC](https://discourse.codecombat.com/t/desert-level-double-cheek-default-code-causes-infinite-loop-error/8926/3 "2017-08-05T15:49:50Z")

</div>

Hi, Bryukh,  
did you fix the problem?

---

<div class="post-metadata">

### Author: ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)
#### Post date: [August 6, 2017, 7:20am UTC](https://discourse.codecombat.com/t/desert-level-double-cheek-default-code-causes-infinite-loop-error/8926/4 "2017-08-06T07:20:50Z")

</div>

Do you get the infinity loop with the default code?
