Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    OM2
    OM2 is offline
    New Coder
    Join Date
    Apr 2012
    Posts
    59
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Trouble accessing variables inside of for loop

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Ummm...

    Code:
    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:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    OM2
    OM2 is offline
    New Coder
    Join Date
    Apr 2012
    Posts
    59
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Quote Originally Posted by OM2 View Post
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    OM2
    OM2 is offline
    New Coder
    Join Date
    Apr 2012
    Posts
    59
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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/1...ue-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

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by OM2 View Post
    numberoffiles[] = i;
    the JavaScript version of that is

    Code:
    numberoffiles.push(i);
    So the proper JavaScript to do the whole thing would be:

    Code:
    "use strict";
    var totalNumberOfFiles, numberoffiles, i;
    totalNumberOfFiles = 10;
    numberoffiles = [];
    
    for (i = 1; i <= totalNumberOfFiles; i++)  {
        numberoffiles.push(i);
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    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:
    Code:
    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
    Code:
    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
    Code:
    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.
    Last edited by Old Pedant; 05-19-2012 at 05:30 AM.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by iBall View Post
    How is using push() more proper than someone using

    Code:
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    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:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    or
    Code:
    for (i = 0; i <totalNumberOfFiles; i++)  {
        numberoffiles.push(i+1);
    }

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Quote Originally Posted by xelawho View Post
    or
    Code:
    for (i = 0; i <totalNumberOfFiles; i++)  {
        numberoffiles.push(i+1);
    }
    No. That gives you the array
    Code:
       [ 1,2,3,4,5,6,7,8,9,10 ]
    Same as Felgall's
    Code:
    for (i = 1; i <totalNumberOfFiles; i++)  {
        numberoffiles.push(1);
    }
    Whereas OM2's original code (with corrections) would produce
    Code:
       [ 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:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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:
    Code:
    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?

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Sorry, typo. Yes, of course, it should be push(i).

    The first line
    Code:
    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
    Code:
       [null,1]
    and after the 10th push(i), you would indeed have
    Code:
       [null,1,2,3,4,5,6,7,8,9,10]
    thus duplicating OM2's original code (as corrected).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    right. if it looked like this it would make a lot more sense
    Code:
    var numberoffiles = [null];
    for (i = 1; i <totalNumberOfFiles; i++)  {
        numberoffiles.push(i);
    }

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by xelawho View Post
    or
    Code:
    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:

    Code:
    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:

    Code:
    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.
    Last edited by felgall; 05-20-2012 at 03:17 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •