Backwoods Forest, Useful Competitors misson ( JS )


#1

The goal of mission is to kill all ogres who (!= peon) and collect all coins and evade poison(!= poison).
My code:

// The coin field has been seeded with vials of deadly poison.
// Ogres are attacking, while their peons are trying to steal your coins!
// Attack the enemy only if the type is NOT equal to “peon”.
while (true) {
var item = hero.findNearestItem();
var enemy = hero.findNearestEnemy();
if (enemy.type != “peon”) {
hero.attack(enemy);
} else if (item.type != “poison”) {
hero.moveXY(item.pos.x, item.pos.y);
}
}

After start, my char staying afk for a while, then goin to kill some ogre and die w/o collecting all coins, or if dont die, misson anyway failed becuz of time. Bug in misson or my code bad ?
Even if i change code to - search item first, like this:

while (true) {
var item = hero.findNearestItem();
if (item && item.type != “poison”) {
hero.moveXY(item.pos.x, item.pos.y);
}
var enemy = hero.findNearestEnemy();
if (enemy && enemy.type != “peon”) {
hero.attack(enemy);
}
}

I can collect all coins, but cant defeat all ogres.


#2

try else if for enemy