Beck
03-02-2003, 06:10 AM
I feel I understand arrays pretty well, at least the javascript semantics of them. I also follow the structure of for-loops adequately. But I'm really stumped by the following piece of code:
var abc = new Array(0,1,1,1,2,3,4,0,6,-1,-1,-1,-2,-3,-4,0,-6);
for (i=0; i < abc.length; i++)
var C=Math.round(Math.random()*[i]);
howbend = abc[C];
Here are my main questions (aside from the overall "what does it do"):
Isn't C declared inside the for-loop, making it "destruct" or die away to nothing upon exiting the for-loop, since it's local to the loop? If so, the last line should send an error message of an undefined object, or at least that C from abc[C] should not have a predictable value (and we're not blaming the random number generator here).
What does [i] mean from the statement inside the for-loop? I feel like it's related to the abc array being used as the conditions for the for loop, but it's not specified, and even if it was, that C is still bugging me.
If it does loop through the entire deal, does each value seen by C somehow have an affect on the final value of howbend, or just that last value computed during the for-loop. To state it another way, from what I see, C should be assigned a different value for EACH ITERATION of the for-loop. Yet it goes through it almost twenty times. Is there some kind of summation taking place, or is the value from the last iteration the only one recorded in C as the loop is terminated?
If you can't answer the questions themselves (and I wuoldn't blame you), are there any references to where I could research this further? Please Advise. Thanks in advance.
var abc = new Array(0,1,1,1,2,3,4,0,6,-1,-1,-1,-2,-3,-4,0,-6);
for (i=0; i < abc.length; i++)
var C=Math.round(Math.random()*[i]);
howbend = abc[C];
Here are my main questions (aside from the overall "what does it do"):
Isn't C declared inside the for-loop, making it "destruct" or die away to nothing upon exiting the for-loop, since it's local to the loop? If so, the last line should send an error message of an undefined object, or at least that C from abc[C] should not have a predictable value (and we're not blaming the random number generator here).
What does [i] mean from the statement inside the for-loop? I feel like it's related to the abc array being used as the conditions for the for loop, but it's not specified, and even if it was, that C is still bugging me.
If it does loop through the entire deal, does each value seen by C somehow have an affect on the final value of howbend, or just that last value computed during the for-loop. To state it another way, from what I see, C should be assigned a different value for EACH ITERATION of the for-loop. Yet it goes through it almost twenty times. Is there some kind of summation taking place, or is the value from the last iteration the only one recorded in C as the loop is terminated?
If you can't answer the questions themselves (and I wuoldn't blame you), are there any references to where I could research this further? Please Advise. Thanks in advance.