anukul anukul - 1 month ago 9
Javascript Question

Error: status[x] is undefined

I want to create a dictionary that can be accessed like

status[a][b]

where
a
and
b
are random strings that are not known at the time of initialization.

Here's the exact use case:

status = {}
name = {};
time = {};
score = {};
for (i=0; i<$scope.submissions.length; i++)
{
e = $scope.submissions[i];
status[e.rno] = status[e.rno] || {};
time[e.rno] = time[e.rno] || 0;
score[e.rno] = score[e.rno] || 0;
status[e.rno][e.problem] = status[e.rno][e.problem] || 0;
if (e.score == 100 && status[e.rno][e.problem] == 0)
{
status[e.rno][e.problem] = 100;
time[e.rno] += e.id;
score[e.rno] += 100;
}
}
console.log(score["20161230"]);


It throws the error:
Error: status[e.rno] is undefined

Answer

status is a window property and you cannot change its type. So you should use var if you are in a function scope (to avoid using the global window property) or use another name if it has to be global.

Comments