# \[SOLVED\] Double cheek infinite loop

**URL:** <https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263>\
**Category:** Level Help\
**Created:** [July 27, 2020, 12:39pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263 "2020-07-27T12:39:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![weary](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/weary/32/15122_2.png) [@weary](https://discourse.codecombat.com/u/weary)\
**Post date:** [July 27, 2020, 12:39pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/1 "2020-07-27T12:39:19Z")

</div>

In the double cheek level I’m having infinite loop problem I don’t know what happened I think the code is correct but is it a bug?  
The Code

```python
// First, defeat 6 ogres.
// Then collect coins until you have 30 gold.

// 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 side 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.
    var item = hero.findNearestItem();
    if (item) {
        hero.move(item.pos);
    }
}

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

```

---

<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:** [July 27, 2020, 12:44pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/2 "2020-07-27T12:44:28Z")

</div>

> [@weary](#):
>
> `defeatedOgres += 1;`

Put this after the else tp that if statement and it should stop the infinite loop.

Andrei

---

<div class="post-metadata">

**Author:** ![weary](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/weary/32/15122_2.png) [@weary](https://discourse.codecombat.com/u/weary)\
**Post date:** [July 27, 2020, 12:49pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/3 "2020-07-27T12:49:01Z")

</div>

Didn’t work. (20 chars)

---

<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:** [July 27, 2020, 12:49pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/4 "2020-07-27T12:49:23Z")

</div>

Can I see your recent code?

Andrei

---

<div class="post-metadata">

**Author:** ![weary](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/weary/32/15122_2.png) [@weary](https://discourse.codecombat.com/u/weary)\
**Post date:** [July 27, 2020, 12:49pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/5 "2020-07-27T12:49:50Z")

</div>

```python
// First, defeat 6 ogres.
// Then collect coins until you have 30 gold.

// 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 side 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.
    var item = hero.findNearestItem();
    if (item) {
        hero.moveXY(item.pos.x, item.pos.y);
    }
}

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

```

I’m not suppose to change anything except add this.

```python
var item = hero.findNearestItem();
    if (item) {
        hero.moveXY(item.pos.x, item.pos.y);

```

---

<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:** [July 27, 2020, 12:50pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/6 "2020-07-27T12:50:24Z")

</div>

> [@weary](#):
>
> `defeatedOgres += 1;`

This one after this

> [@weary](#):
>
> ```python
> hero.say("Ogres!");
> }
> 
> ```

Andrei

---

<div class="post-metadata">

**Author:** ![weary](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/weary/32/15122_2.png) [@weary](https://discourse.codecombat.com/u/weary)\
**Post date:** [July 27, 2020, 12:53pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/7 "2020-07-27T12:53:10Z")

</div>

Thank you so much it worked I think its some problems with the developers because I didn’t change except add find the items and collect them.

---

<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:** [July 27, 2020, 12:57pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/8 "2020-07-27T12:57:41Z")

</div>

No problem! Anytime! The problem was that it was stuck when it found a dead enemy because it will not go to the next one.

Andrei

---

<div class="post-metadata">

**Author:** ![weary](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/weary/32/15122_2.png) [@weary](https://discourse.codecombat.com/u/weary)\
**Post date:** [July 27, 2020, 12:58pm UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/9 "2020-07-27T12:58:01Z")

</div>

Make sense. (20 chars)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [July 28, 2020, 12:58am UTC](https://discourse.codecombat.com/t/solved-double-cheek-infinite-loop/23263/10 "2020-07-28T00:58:04Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
