[SOLVED] Chameleon Level - Javascript - Error despite Success?

while (true) {
    var enemy = hero.findNearestEnemy();
    // If you see an enemy - attack it:
        if (enemy) {
            hero.attack(enemy);
        }
    var item = hero.findNearestItem();
    // If you see a coin or a gem - move to its X and Y position:
    var itemPos = item.pos;
    var itemX = item.pos.x;
    var itemY = item.pos.y;
    hero.moveXY(itemX, itemY);

I get level success, however I get an error on line 11 “TypeError: Cannot read property ‘pos’ of null” which I don’t understand. It refers to “var itemPos = item.pos;” if I remove this line, I still get success, but I instead get the error on the next line “var itemX = item.pos.x;” Very confusing and I don’t want to move on until I understand what I’m doing wrong!

add a if item { after the line

OH. Silly me. Thank you!! :slight_smile:

2 Likes