Reputation: 9
[![enter image description here][1]][1]I have been struggling with F--king arrays and objects and they will not display to browser.
I have used a constructor to build the array and then pushed it to the array inventory. I've been fiddling with this for days and I just don't get it. This keeps stopping me in all my projects, the minute I try to access an array I get undefined All I've ever gotten is Undefined. I really don't understand what i've done wrong.
Upvotes: 0
Views: 42
Reputation: 8087
Your for loop says:
for (var i = 0; i <= inventory.length; i++)
But that will go past the last item in the array, because the last item in the array will have the index inventory.length-1
and not inventory.length
.
Instead, do:
for (var i = 0; i < inventory.length; i++)
Upvotes: 2