I have the following function:
function get_stoplight_color(count, position) {
console.log(count + ', ' + position);
var colors = {
'QB' : ['#8b3a25', '#ceca63', '#68c24c', '#68c24c', '#68c24c'],
'RB' : ['#8b3a25', '#ceca63', '#ceca63', '#ceca63', '#68c24c'],
'WR' : ['#8b3a25', '#ceca63', '#ceca63', '#ceca63', '#ceca63'],
'TE' : ['#8b3a25', '#ceca63', '#68c24c', '#68c24c', '#68c24c'],
'DST' : ['#8b3a25', '#68c24c', '#68c24c', '#68c24c', '#68c24c'],
'K' : ['#8b3a25', '#68c24c', '#68c24c', '#68c24c', '#68c24c']
};
console.log(colors);
if (count <= 4) return colors.position[count];
else return '#68c24c';
}
As you can see, I've logged a couple vars out to the console, and they appear to be just fine there. However, at runtime I'm getting the following error:
Uncaught TypeError: Cannot read property '4' of undefined
(That's when count = 4.)
Again, the object colors clearly exists, because console.log(colors) outputs:
Object {QB: Array[5], RB: Array[5], WR: Array[5], TE: Array[5], DST: Array[5]…}
So I would expect colors.position[count] to return #68c24c when position = 'QB' and count = 4.
Why is the object still returning undefined in this case? Thank you, and I'll provide more details if necessary (I've provided everything I've thought of up front).