I get error on line 7: TypeError. Cannot read property ‘type’ of null.
The Objective of this level are:
// Ogres are attacking a nearby settlement!
// Be careful, though, for the ogres have sown the ground with poison.
// Gather coins and defeat the ogres, but avoid the burls and poison!
This was the code given to you.
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy.type == "munchkin" || enemy.type == "thrower") {
hero.attack(enemy);
}
This was the code I wrote to have my character check to see if the item was coin or a gem.
var item = hero.findNearestItem();
var pos = item.pos;
var x = pos.x;
var y = pos.y;
// Check the item type to make sure the hero doesn't pick up poison!
// Look for types: 'gem' and 'coin'
if (item == "coin" || "gem") {
hero.moveXY(x, y);
}
The full code looks like this:
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy.type == "munchkin" || enemy.type == "thrower") {
hero.attack(enemy);
}
var item = hero.findNearestItem();
var pos = item.pos;
var x = pos.x;
var y = pos.y;
if (item == "coin" || "gem") {
hero.moveXY(x, y);
}
}
I also noticed a few things with this level. No throwers or gem’s spawn throughout the level while running. And the directions never mention picking up gems or killing throwers, but before you start the level it does and the hints say it as well.
Update: Also tried this
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy.type == "munchkin" || enemy.type == "thrower") {
hero.attack(enemy);
}
var item = hero.findNearestItem();
var pos = item.pos;
var x = pos.x;
var y = pos.y;
if (item.type == "coin" || item.type == "gem") {
hero.moveXY(x, y);
}
}
as well as this
while(true) {
var enemy = hero.findNearestEnemy();
if(enemy.type == "munchkin" || enemy.type != "burl") {
hero.attack(enemy);
}
var item = hero.findNearestItem();
var pos = item.pos;
var x = pos.x;
var y = pos.y;
if (item.type == "coin" || item.type != "poison") {
hero.moveXY(x, y);
}
}