I’m getting an error through a piece of script when using variables in setting an array. If I just replace the variables with number values they have (which I have verified) I don’t get an error.
Is there anything wrong with setting an array like this?
arrayOfData = new Array(
[leftAmount, 'Get', '#2697C9'],
[middleAmount, 'Neutral', '#E7E7E7'],
[rightAmount, 'Don\'t Get', '#EB5830']
);
You can see the whole function if it’s helpful
function generateChart(viewerObj){
if(viewerObj.getActiveUsers){
var leftAmount = viewerObj.getActiveUsers;
}
else{
window.leftAmount = 0;
}
if(viewerObj.getActiveUsers){
var middleAmount = viewerObj.getActiveUsers;
}
else{
var middleAmount = 0;
}
if(viewerObj.dontGetActiveUsers){
var rightAmount = viewerObj.dontGetActiveUsers;
}
else{
var rightAmount = 0;
}
arrayOfData = new Array(
[leftAmount, 'Get', '#2697C9'],
[middleAmount, 'Neutral', '#E7E7E7'],
[rightAmount, 'Don\'t Get', '#EB5830']
);
$('.divGraph').jqBarGraph({ data: arrayOfData });
}
Only potential issue I can see is that most of the time you have…
…in both the
ifandelse, but one time you have this……therefore when the
ifcondition fails, the localleftAmountwill beundefined, and will be shadowing the globalleftAmount, which has the0value.I’d guess that wherever you’re using that Array, it doesn’t like the
undefinedsubstitute for0.Change
window.leftAmounttovar leftAmount, or better, move your variable declarations to the top of the function.Here I also set the default initialization at the top. If you prefer, you can move it back to the
else, or you can use the conditional operator…Or since you’re doing a basic truthy/falsey test, you could do this…