# [SOLVED] Double cheek infinite loop

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

``````// 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);

``````

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

Andrei

1 Like

Didn’t work. (20 chars)

Can I see your recent code?

Andrei

1 Like
``````// 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.

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

This one after this

Andrei

1 Like

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.

1 Like

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

Make sense. (20 chars)

1 Like

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