javascript array error: array[n] is undefined
relevant code:
function stepNetwork() {
for(i = 0; i<maxNeurons; i++) {
if(neuronArray[i].charge >= neuronArray[i].threshhold) {
var elapsed = ((new Date()).getTime()) - neuronArray[i].lastFired;
if(elapsed >= 5000){
fireNeuron(i);
}
}
}
}
function fireNeuron(n){
//get number of outputs on this neuron
var outs = neuronArray[n].outputs.length;
for(p = 0; p < outs; p++) {
sendChargeTo = neuronArray[n].outputs[p];
addCharge(sendChargeTo);
}
neuronArray.charge = 0;
neuronArray.lastFired = ((new Date()).getTime());
}
function addCharge(n) {
neuronArray[n].charge++;//HERES THE ERROR!!
}
Here is what firebug is telling me:
neuronArray[n] is undefined ///then why can I see its value in the scripts->watch tab?
addCharge(n=100)js_operation.php (line 73)
fireNeuron(n=73)js_operation.php (line 66)
stepNetwork()
The 开发者_StackOverflow社区thing that gets me is that when I pass a number it works, and when I evaluate neuronArray
, neuronArray[n]
, neuronArray[n]
. charge
etc in the scripts pane (watch area), it always is able to reference it.
Maybe this is the problem:
sendChargeTo = neuronArray[n].outputs[p];
addCharge(sendChargeTo);
You're not sending addCharge
an index, you're sending it neuronArray[n].outputs[p]
.
Apart from Raynos's comments about the last two lines in fireNeuron
I can't see an obvious problem.
Perhaps you have a globals problem? I think that variables i
, p
, and sendChargeTo
should be declared local with var
.
精彩评论