View Full Version : a for loop/array question

03-02-2003, 07: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.

03-02-2003, 07:52 AM
javascript has no "block level", so C remains defined once that loop has ended. as for that [i], i don't know what that is; that should be throwing an error, as far as i know. it certainly looks like it's supposed to have to do with the array

c is being assigned a new value on every iteration. the new value replaces the old value.

Graeme Hackston
03-02-2003, 07:55 AM
I don't know what it's for, I'd guess it's generating random numbers.

This will show you what C and howbend are with each pass through the loop. Because I made them global, when the loop is complete, they'll take the value of the last pass through the loop.

C = ''
howbend = ''
array2 = new Array()
array3 = new Array()
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];
array2[array2.length] = C
array3[array3.length] = howbend
alert('C = \n\n'+array2.join('\n'))
alert('howbend = \n\n'+array3.join('\n'))
alert('C = '+C)
alert('howbend = '+howbend)

i is set to 0 when the loop starts then incremented by 1 with each pass. When i equals abc.length the loop stops.

Graeme Hackston
03-02-2003, 08:06 AM
John is right. I didn't notice that [i]. Now I'd like to know why [i] isn't throwing an error.

Graeme Hackston
03-02-2003, 08:14 AM
Just thinking, it should mulitply i by Math.random if it's written like this

var C=Math.round(Math.random()*i);

Maybe it's just unrequired brackets that still function, something like this

if ((C=='')) {

03-02-2003, 08:16 AM
yeah, that would work. the brackets would make it an array literal. which would make absolutely no difference in this particular case.

03-04-2003, 12:43 AM
Best to take it row by row here:

var abc = new Array(0,1,1,1,2,3,4,0,6,-1,-1,-1,-2,-3,-4,0,-6);
// Bet you understand this.
//Creates an array with the given values as members.
for (i=0; i < abc.length; i++)
//Bet you understand this as well
//This loop construct is the basic for loop as given in as example in book etc.
var C=Math.round(Math.random()*[i]);
// Don't understand why anyone would do this -
// it's in effect only the last value of i that will be used,
// as C is overwritten each turn through the loop;
// in other words, the index of the last element in the array is the only value of i that is used.
// The brackets around i have no effect whatsoever due to the fact that
// JavaScript does silent type conversion when needed if possible,
// and a one-element array can be type-converted to it's content type.
howbend = abc[C];
// In the end, howbend is assigned the value of a random element in the abc array.
// This could have been done far easier, so I guess the author doesn't really understand what he/she is doing.

If I were to make a guess, this code is supposed to create a biased random value from a set - if so, there are better and easier ways of doing it, here an example:

var howbend=(Math.round(Math.random())||-1)*[0,1,1,1,2,3,4,0,6][Math.round(Math.random()*8)];

03-04-2003, 12:22 PM
thanks to all who replied and conversed over this. Made me feel warm inside :thumbsup:

I have the original script and author's email. I'll ask him what he's expecting to come out of this. Maybe I'll post when I get an answer, decide it's worth sharing, and can still find this posting in the forum.

liorean - thanks for that last bit of more efficient looking code, I'll use that. (Showoff! ;) )

03-04-2003, 01:33 PM
It seems the writer of the script has an email server past quota, so that scratches that. Now I guess we'll never know :)