Lang: Javascript - Board: Gold Rush
I understand programming logic, but I’m lost when comes to language specifics. I’m trying to loop through the 1st 5 objects in the array and pick the one with the biggest bountyGold.
Code:
var items = this.getItems(); //create array of items
var itemValue = 0;
// find best of closest 5
for(var i = 0; i < 4; ++i) {
var item = items[i];
if (item.bountyGold > itemValue) { //if it's better, use it| BTW this is line 7
itemValue = item.bountyGold;
var itemPos = item.pos;
}
}
// Strip coordinates
itemX = itemPos.x;
itemY = itemPos.y;
this.say(itemValue);
Error:
I keep getting an error at line 7 saying that “item” is null.
I have no idea what is wrong with my code, but I’m sure it’s something stupid. Any help would be appreciated.
Thanks in advance,
Josh