Desert Level Double Cheek - default code causes infinite loop error

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

Error Image

Code:

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:

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

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.

Hi, Bryukh,
did you fix the problem?

Do you get the infinity loop with the default code?