I am trying to optimize using for loops. Right now I use nested IF statements for most things and I would like to clean that up and begin replacing a lot of that messy code with for loops. In this example I have nested arrays. What I am trying to do is have a loop that evaluates each array under shapes and when it finds a value that matches with choice, it returns the shape name (triangle, square, or circle) which in the particular case would be square. The following has not worked obviously so I am hoping someone can help correct me and teach me where I went wrong.
What I have tried so far is the following:
function getShape()
choice = "cube"
local shapes = {
["triangle"] = {"right", "acute", "obtuse"},
["square"] = {"rectangle", "cube", "rhombus"},
["circle"] = {"tangent", "concentric", "congruent"},
}
for k,v in pairs(shapes) do
for i = #v, 0, -1 do
if v[i] == choice then
return k
break
end
end
end
end
I don’t get any errors, I just get no output from it.
Robert Willey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.