AlwaysNeedingHelp
AlwaysNeedingHelp

Reputation: 1945

Access loop variable from within function

Not the greatest title i know, alright, consider:

for (var i = 0; i < map.length; ++i) {
place_ore(mountain_ore,mountain_allowed_ores) 
}

And inside place_ore(), I am trying to access map[i];, however when I try to do this, it gives me an undefined error. I think it has something to do with scope, but I can't quite work it out myself, any ideas?

Thanks.

Upvotes: 0

Views: 120

Answers (1)

Jaime Torres
Jaime Torres

Reputation: 10515

You need to pass it in:

for (var i = 0; i < map.length; ++i) {
    place_ore(mountain_ore,mountain_allowed_ores, map[i]) 
}

And of course, modify your function signature:

function place_ore(mountain_ore,mountain_allowed_ores, mapTile) {
   //..place some ore in mapTile instead of map[i]
}

Upvotes: 3

Related Questions