...

View Full Version : Trouble accessing variables inside of for loop



OM2
05-19-2012, 03:10 AM
I have the following code:

...
var numberoffiles = array();

for (i = 1; $i <= totalNumberOfFiles; i++)
{
numberoffiles[i] = i;
}
...

I get an error saying that numberoffiles is undeclared or something
What am I doing wrong?
Do I need to declare it a global variable or something??

ALSO, why can't I do this:

numberoffiles[] = i;

I've seen code snippets that assign to the end of an array like this
(Or maybe my problem is the scope of the variable??)

Thanks


OM

Old Pedant
05-19-2012, 03:21 AM
Ummm...



var numberoffiles = array();
for (i = 1; $i <= totalNumberOfFiles; i++)
{
numberoffiles[i] = i;
}

numberoffiles isn't defined, because your assignment that created it was bogus.

You needed:


var numberoffiles = new Array(); // you need the new operator and upper case A
// or you could have done simply
var numberoffiles = [];

And then where did you find the $i that you slipped in, in place of just plain i??

**********

Also...



numberoffiles[] = i;

??? HUH? Where did you ever see that being legal???

Find an example, please.

**********

EDIT: Well, iBall beat me, for once. But he missed the $i and didn't mention the other point, so I won't delete this.

OM2
05-19-2012, 03:22 AM
thanks
it still complains that the array is undefined
i now have:

var totalNumberOfFiles = 10;
var numberoffiles = new array();

for (i = 1; $i <= totalNumberOfFiles; i++)
{
numberoffiles[i] = i;
}

error reported: "Uncaught ReferenceError: array is not defined"

any ideas?
thanks

Old Pedant
05-19-2012, 03:24 AM
thanks
it still complains that the array is undefined
i now have:

var totalNumberOfFiles = 10;
var numberoffiles = new array();

for (i = 1; $i <= totalNumberOfFiles; i++)
{
numberoffiles[i] = i;
}

error reported: "Uncaught ReferenceError: array is not defined"

any ideas?
thanks

Yes. READ THE REPLIES you get. Array. Not array. JavaScript is case sensitive. You can not just change upper to lower and vice versa as you please.

And you STILL have that bogus $i in there.

OM2
05-19-2012, 03:35 AM
thanks for the replies guys
the $ - erm, it must have been a gremlin - i didn't put it there
(ok - so, i'm a newbie - i admit it!!)

with regards to the array assignment - i misread!
i googled for 'how to add to end of array javascript'
and saw this link:

http://stackoverflow.com/questions/1996747/add-value-to-an-array

i didn't realise that the questioner was saying this is what u do in php (i was reading too fast and thought u could do in JS)

a little but more experienced now thanks to u guys :)

EDIT: i was busy replying to replies - didn't realise other replies with solutions had come in already - i was definitely reading and listening to ur advice

felgall
05-19-2012, 04:31 AM
numberoffiles[] = i;

the JavaScript version of that is


numberoffiles.push(i);

So the proper JavaScript to do the whole thing would be:


"use strict";
var totalNumberOfFiles, numberoffiles, i;
totalNumberOfFiles = 10;
numberoffiles = [];

for (i = 1; i <= totalNumberOfFiles; i++) {
numberoffiles.push(i);
}

Old Pedant
05-19-2012, 06:26 AM
I really really hate to agree with iBall, but...

On this one he happens give a right answer, but not for the reason he puts forth.

In particular, he stated

Both work in all the major browsers and do exactly the same thing in the FOR loop.


But in point of fact, they don't do the same thing, at all.

********

The problem is that OM2's loop looks like this:


for (i = 1; i <= totalNumberOfFiles; i++) {
numberoffiles[i] = i;
}

So he NEVER PUTS ANY VALUE into the numberoffiles[0] !!!

Possibly he is used to other languages that don't have a zero element in arrays (pretty old languages, but possible).

Possibly he has some particular reason to leave element zero empty.

Or possibly he just goofed.

But assuming he really did want to leave element zero empty, then you can't easily use

for (i = 1; i <= totalNumberOfFiles; i++) {
numberoffiles.push(i);
}

Because now, of course, numberoffiles[7] will contain 8, just for example.

Yes, yes, I know you COULD just change the loop to


for (i = 0; i <= totalNumberOfFiles; i++) {
numberoffiles.push(i);
}

But now numberoffiles[0] contains 0 instead of null, as it would in OM2's code.

I'm guessing that OM2 goofed, and he really would have started with i = 0 if he had thought about it. But I can't be sure of that.

felgall
05-19-2012, 09:55 AM
How is using push() more proper than someone using


numberoffiles[i] = i;

Well that was answering the question about a JavaScript equivalent to the PHP

$numberoffiles[] = $i;

where using push() is the JavaScript equivalent while the alternative is not - particularly with a loop where i goes from 1 through 10 instead of 0 through 9 where the two JavaScript codes you ask about do not do the same thing.

You really ought to read the OP's question more thoroughly before posting irrelevant comments about the answer.

As for my saying "the proper way" I of course meant :the closest proper way to the existing code that would exist after substituting the equivalent to that PHP statement that the OP was asking about" - there are of course at least several dozen different equally proper ways to produce that code but all of the others differ more from the code that the OP was asking about/for and so would involve making bigger changes to the existing code - and why change the code more than necessary in fixing it so it is coded properly.

Old Pedant
05-19-2012, 11:41 PM
I don't see how we can answer the push vs. subscript question definitely until and unless OM2 explains why he left element zero of the array null. Or if he cares.

Truthfully, for an array of only the numbers 1 to 10 (or 0 to 10), I'd just do:


var numberoffiles = [1,2,3,4,5,6,7,8,9,10];

But of course that assumes you know how many you need ahead of time.

xelawho
05-20-2012, 12:09 AM
or

for (i = 0; i <totalNumberOfFiles; i++) {
numberoffiles.push(i+1);
}
:)

Old Pedant
05-20-2012, 02:16 AM
or

for (i = 0; i <totalNumberOfFiles; i++) {
numberoffiles.push(i+1);
}
:)

No. That gives you the array


[ 1,2,3,4,5,6,7,8,9,10 ]

Same as Felgall's

for (i = 1; i <totalNumberOfFiles; i++) {
numberoffiles.push(1);
}

Whereas OM2's original code (with corrections) would produce


[ null,1,2,3,4,5 6,7,8,9,10]

As I said, *IF* it is important to him to have a null first element, then his (corrected) answer is the simplest way.

You could do:


var list = [null];
for (i = 1; i <totalNumberOfFiles; i++) {
numberoffiles.push(1);
}
to duplicate the null first element, of course.

I say again: it really depends on what your ultimate aim is.

xelawho
05-20-2012, 03:31 AM
yeah, I was just offering another way of making the [ 1,2,3,4,5,6,7,8,9,10 ] array.

this on the other hand:


var list = [null];
for (i = 1; i <totalNumberOfFiles; i++) {
numberoffiles.push(1);
}


I don't understand at all. I see that the 1 should probably be an i but what is the list var doing?

Old Pedant
05-20-2012, 03:39 AM
Sorry, typo. Yes, of course, it should be push(i).

The first line

var list = [null];

is, effectively, creating an array with one element (i.e., element number zero) where the value of the element is indeed null.

After the first push(i) [where i starts out at 1], you would thus have

[null,1]
and after the 10th push(i), you would indeed have


[null,1,2,3,4,5,6,7,8,9,10]
thus duplicating OM2's original code (as corrected).

xelawho
05-20-2012, 03:43 AM
right. if it looked like this it would make a lot more sense


var numberoffiles = [null];
for (i = 1; i <totalNumberOfFiles; i++) {
numberoffiles.push(i);
}

felgall
05-20-2012, 04:13 AM
or

for (i = 0; i <totalNumberOfFiles; i++) {
numberoffiles.push(i+1);
}
:)

That version is less efficient because each time around the loop you have to add one to i twice instead of once.

The benefit of:


a = 12;
b = 25;
for (i = a; i <= b; i++) {
numberoffiles.push(i);
}

is that it loads the numbers in the specified range a through b into the array without leaving any undefined entries and without needing to do two calculations each time around the loop. push simply inserts on the end of what is already there.

The alternative:


a = 12;
b = 25;
for (i = a; i <= b; i++) {
numberoffiles[i] = i;
}

places the numbers in the positions in the array corresponding to their numbers and leaves all the positions between 0 and a-1 undefined.

Note that in JavaScript array entries that have not been assigned values are undefined numberoffiles[0]!==null. All the above posts specifying NULL values in the array are incorrect except in the examples where the code shows the value being set to null - although why anyone would bother setting it to null when undefined serves the same purpose I don't know.

Old Pedant
05-20-2012, 04:49 AM
Yeah, I know null and undefined aren't the same thing. I should have been more exacting in my comments.

I like your last example, going from a to b, because it really emphasizes what I was saying. To wit, if you didn't want the elements below a to be initialized, they won't be.

I still think that OM2 just goofed in his original code and should have started at zero, but it looks like all the bickering have driven him off, so we may never know for sure. Partly mea culpa. Teach me to try to be pedantic.

OM2
05-21-2012, 04:08 AM
guys: thank u for some awesome replies :)
all the banter above was in good nature (i hope i didn't start a big fight or anything!)

YES: it was a giant goof on my behalf
i didn't mean to leave the first element as zero!
in my other thread (which i though was a separate subject, hence i posted separately) i was getting errors - some of the time only! this now i know was because of the null when random was choosing from the list of numbers!

the code i have now is:


var totalNumberOfFiles = 10;
var numberoffiles = new Array();
for (i = 1; i <= totalNumberOfFiles; i++)
{
numberoffiles.push(i);
}

i've checked in console.log what the array contains - i definitely do NOT want a null in the beginning

all i wanted was an array with the following contents [1,2,3,4,5,6,7,8,9,10]
i wanted to fill automatically as i would change the end number 10 frequently to be a different number

question which is more optimal in execution:

numberoffiles.push(i);

or

numberoffiles[i] = i+1;

??

thanks

Old Pedant
05-21-2012, 07:47 AM
That was answered by Felgall in post #22



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum