// Defeat munchkins, collect coins. Everything as usual.
// Use AND to check existence and type in one statement.
while (true) {
var enemy = hero.findNearestEnemy();
// With AND, the type is only checked if enemy exists.
if (enemy && enemy.type == "munchkin") {
hero.attack(enemy);
}
// Find the nearest item.
var item = hero.findNearestItem();
// Collect item if it exists and its type is "coin".
if(item.type == "coin") {
hero.moveXY(item.pos.x, item.pos.y);
}
}
// Defeat munchkins, collect coins. Everything as usual.
// Use AND to check existence and type in one statement.
while (true) {
var enemy = hero.findNearestEnemy();
// With AND, the type is only checked if enemy exists.
if (enemy && enemy.type == "munchkin") {
hero.attack(enemy);
}
// Find the nearest item.
var item = hero.findNearestItem();
// Collect item if it exists and its type is "coin".
if (item) {
if(item.type == "coin") {
hero.moveXY(item.pos.x, item.pos.y);
}
}
}
Both are wrong.
-
It says that it can’t just have
item.type == "coin"
. -
I have 6 statements because of
if (item)
If someone could please respond quickly, that would be a big help. Thanks!