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 22

Thread: Array Noob

  1. #1
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts

    Question Array Noob

    Do arrays use the number you type in a form field and match Index numbers or does the array count down the number of array objects with the number you typed in?
    When you type a number to a text field to search in an array, does it look for an exact number match, or does it count down the number of array items with the number you typed in?

    For Example, When I type in a number 4 in a form field, am I asking asking JavaScript to find the index number 4, i.e., find an exact match between the string "6" typed in the form to the 6 in the bracket [6]) OR am I asking JavaScript to count down four array items?
    Last edited by ryanjohnsond; 06-15-2012 at 01:23 AM.

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    47
    Thanks
    1
    Thanked 15 Times in 14 Posts
    It is matching only the Index number

    You can see this if you run this very simple example I put together here: http://jsfiddle.net/6Gpyg/
    Last edited by jimutt; 06-14-2012 at 10:51 PM.

  • Users who have thanked jimutt for this post:

    ryanjohnsond (06-15-2012)

  • #3
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jimutt View Post
    It is matching only the Index number

    You can see this if you run this very simple example I put together here: http://jsfiddle.net/6Gpyg/
    If it only matches the index number, and I type in 12 (for December) in a text field, won't I get a message that 12 is "undefined"?

    Function MonthAsString(NUM)
    {
    var aryMonths = new Array();
    aryMonths[0] = "January";
    aryMonths[1] = "February";
    aryMonths[2] = "March";
    aryMonths[3] = "April";
    aryMonths[4] = "May";
    aryMonths[5] = "June";
    aryMonths[6] = "July";
    aryMonths[7] = "August";
    aryMonths[8] = "September";
    aryMonths[9] = "October";
    aryMonths[10] = "November";
    aryMonths[11] = "December";
    }

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    So I think the issue is a bit of conceptual mismatch.

    Remember that code is not what you type in (to the computer)... its registers and binary and complex stuff you shouldn't even bother thinking about.

    When you enter 12 into your input and hit enter (assuming the JS then puts that 12 into MonthAsString(12)), it doesn't count or search then go back one. What you've done is create a set of data 12 long (mind you, the terms are different, im just simplifying it). When you return aryMonths[NUM-1], THEN it searches the array. But first it needs to know what to search, so it computes NUM-1, getting 11. It then goes through the dataset, knows what 11 is, and returns December.

    So it doesn't go through the array counting (the data may not even be stored in order in the RAM), nor does it go to the number you enter then go back one. Once you create the array, the dataset, it is stored away. First it computes what you're looking for, which is NUM - 1. For 12, that means 11, for 4 it means 3. Then it searches for the array value at that key position (11 and 3 respectively), and returns that value (April and December). To break it, you'd have to enter a final array key beyond what is stored in the array, so something greater then 12 or less then 0 (after the NUM - 1, it has to be outside the range). The array is not read in any human sense... remember, when you create the array, its kind of like writing down names in a book... they don't mean anything until you think of a name to go find.

    I hope that explains it... if not, I'll give it another go.


    I thought of an analogy:
    Its like an address book with an index. Lets say you have a book with a bunch of names, addresses, phone numbers, etc, and lets say you have 1000 names in random order. You're not gonna read the book every time you want a phone number, starting at the beginning and reading every single name. It makes a lot more sense to make an index in the beginning that has each name and the page its on. Then when you want a number, you'll look up the persons name in the index and jump straight to the name, getting that information straight out.

    Thats basically how the code works. Once you create the array, its stored into RAM, with an internal "index" telling it what different things mean. And just how you wouldn't look for Jason THEN realize you want to actually find out who's before Jason, the code doesn't look for some number you enter (remember, it doesn't know what that number means, you tell it what to do with that number) and search for it THEN realize you want to subtract one. It knows you're looking for something in the brackets, so it figures out what that is, in your case, NUM -1, and then goes to search for NUM - 1.

    This is a bit of an abstraction to how it works, so please don't take it as dogma, it needs a lot of refinement. And if it sounds like I'm talking down to you, I really don't mean to, I'm just trying to simplify it as best as I can.
    Last edited by Keleth; 06-14-2012 at 11:55 PM.

  • Users who have thanked Keleth for this post:

    ryanjohnsond (06-15-2012)

  • #5
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    So it doesn't go through the array counting (the data may not even be stored in order in the RAM), nor does it go to the number you enter then go back one. First it computes what you're looking for, which is NUM - 1. For 12, that means 11, for 4 it means 3. Then it searches for the array value at that key position (11 and 3 respectively), and returns that value (April and December). To break it, you'd have to enter a final array key beyond what is stored in the array, so something greater then 12 or less then 0 (after the NUM - 1, it has to be outside the range).

    I hope that explains it... if not, I'll give it another go.
    I thought Javascript is a run time translator, so that it always goes from top, down. (Wouldn't it make sense to put the Num-1 first before the array? that way it would have the correct number to start with, because the top - down runtime of JS is what' throwing me to track how the translator is working) Anyway, it gets 12 looks for index no. 12. and says it's "undefined", goes to the next bit of code that says, "hey! take NUM (which has the string "12" in it) and subtract 1 from it, making it "11". Then it would start over. Is that what it does? Or It just runs through the whole thing resolves all the issue of the NUM-1 then goes through the array in like one swoop?

    If you type in 3 in this example, you get "undefined". http://jsfiddle.net/6Gpyg/
    So, if you type in 12 in my code, you get "undefined" because there is no index 12 to match.
    Last edited by ryanjohnsond; 06-14-2012 at 11:56 PM.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by ryanjohnsond View Post
    I thought Javascript is a run time translator, so that it always goes from top, down. (Wouldn't it make sense to put the Num-1 first before the array? that way it would have the correct number to start with, because the top - down runtime of JS is what' throwing me to track how the translator is working) Anyway, it gets 12 looks for index no. 12. and says it's "undefined", goes to the next bit of code that says, "hey! take NUM (which has the string "12" in it) and subtract 1 from it, making it "11". Then it would start over. Is that what it does? Or It just runs through the whole thing resolves all the issue of the NUM-1 then goes through the array in like one swoop?

    If you type in 3 in this example, you get "undefined". http://jsfiddle.net/6Gpyg/
    So, if you type in 12 in my code, you get "undefined" because there is no index 12 to match.
    Well, it is a run time translator, but that's for actual code, not data. Data is stored away, to be used when its called. There is no difference to the code whether you make an array, and object, etc... (there is some difference, but more complex). An array is just a collection of other objects.

    In the example given above, 3 is undefined because 3 doesn't exist. However, in your example, you're not looking for 12, you're actually looking for 11.

    The data is stored away and referenced when its called. And when you call arymonth[NUM - 1], the array never sees 12... it see NUM -1, which is 11.

    See the example I added above after you posted.

  • Users who have thanked Keleth for this post:

    ryanjohnsond (06-15-2012)

  • #7
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    Its like an address book with an index. Lets say you have a book with a bunch of names, addresses, phone numbers, etc, and lets say you have 1000 names in random order. You're not gonna read the book every time you want a phone number, starting at the beginning and reading every single name. It makes a lot more sense to make an index
    Thanks, Keleth!

    That was a very good anology. I don't think you're talking down. Thanks. To summarize then, [12] is an index number used for looking up things in a list of items. It's an easy way to look up any item based on it's index number. Values in an array can change, but an index number can remain the same. For example, the index for your friend Jason says his info is on page 12, but he recently moved and you crossed out his old number and penciled in his new. But he stayed on the same page and you can easily find him again, based on the index at the front of your address book. Makes perfect sense.

    So, when I type in 4 in a text field, it goes into the array, it looks for the index number of 4, it makes a match and returns the string value. Correct?

    I didn't realize it at the time, but it's sort of become apparrent that I was confused on the run-time, from top-down aspect of the JS translator. My confusion was: "If the NUM-1 comes after the array, how does the translator know to go back up to the array again and grab index number 11?" The translator readsand just reads the code and performs when it hits things and moves down to the next. It should have thrown an "undefined" and then gone to the NUM-1.
    Last edited by ryanjohnsond; 06-15-2012 at 11:20 AM.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by ryanjohnsond View Post
    So, when I type in 4 in a text field, it goes into the array, it looks for the index number of 4, it makes a match and returns the string value. Correct?
    In your code, not quite... since your code search for the index NUM - 1... so if you put 4 into the text field, you're actually searching for 3.

    Quote Originally Posted by ryanjohnsond View Post
    The translator readsand just reads the code and performs when it hits things and moves down to the next. It should have thrown an "undefined" and then gone to the NUM-1.
    The parser more or less reads the code and does some preprocessing first, like making sure there are no fatal errors, that it understands everything, and storing variables, etc. So when you run the code, it does each line sequentially, but only stuff it has to "figure out". But I think that last line is still in a bit of confusion. If you enter 12, the system will never search for index 12 because as far as its concerned, you never ask it to. You ask it find index NUM - 1... so if you give num a value of 12, its searching for index 11.

  • Users who have thanked Keleth for this post:

    ryanjohnsond (06-15-2012)

  • #9
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    So when you run the code, it does each line sequentially...If you enter 12, the system will never search for index 12 because as far as its concerned, you never ask it to.
    Sorry to beat a dead horse, but could you explain how the translator thinks through or steps through the process of how it knows to look for index 11? If it's sequential how does it know to look for index 11, when the NUM-1 is at the bottom?

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Again, the key is preprocessing.

    When you load a page, the browser runs its JS processor, which reads all the JS code on the page. It looks for functions and creates links, it looks at variables and stores them, it reads the code to make sure it understands whats going on in general. Now Javascript is a bit unique in that it won't critically fail over bad code from the get to... it fails when you try to call that code. But besides that, it does general checking to make sure it can do something.

    As for accessing the index, precedence is a big deal, just like math.

    If you have 4 * (3 + 6 / 2 * (9 - 3)) you have order of precedence, in that you look at the inner most parenthesis first, then head outwards. In the same way, code is always by order of precedence, in that inner most code is computed first (most often) and then outer code.

    So if you have monthArray[NUM - 1] first it figures out NUM - 1 (the inner set of code). It doesn't care that its looking for an array index yet, it just knows NUM - 1 has to be calculated. So then it realizes that for NUM = 12, that is 11, so it goes out a layer and sees monthArray[11], and realizes, oh, this is an array, let me try to pull the value of monthArray indexed at 11.

    The fact that its at the bottom doesn't matter, because the only thing above is a variable, data, which is already processed. The variable has to come first because you have to have a variable defined to look for it. It would kind of be like you not having an address book, and someone asking you for Johns address, then the next day you writing an address book... doesn't help you figure out the address yesterday, does it? So with the variable defined above it doesn't mean the variable comes "first"... its just defining a variable. Its writing your address book. Then later, the action is called on the variable, on your address book. If you write your address book but never have to look up a number, the address book doesn't cease to exist... but if you do want to look up something at some point, you need the address book to exist first.

    Order does matter in that lets say above the array definition, you added in NUM = NUM - 3. Then the array call at the bottom instead of being NUM - 1 is more like (NUM - 3) - 1. But if you put NUM = NUM - 3 after the array call, it wouldn't matter, because you've already called the array.

    Its obviously hard to relate the function into this... I hope I'm making it clearer, even if one step at a time. Again, this is a vast oversimplification, and elements are probably technically wrong, I'm just trying to provide a conceptual framework.
    Last edited by Keleth; 06-15-2012 at 04:33 PM.

  • Users who have thanked Keleth for this post:

    ryanjohnsond (06-15-2012)

  • #11
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts
    That clears things up. So, "return" is always the inner most code in precedence? Is there a rule of precedence in JavaScript I can read somewhere?

  • #12
    New Coder
    Join Date
    Jun 2012
    Location
    San Diego
    Posts
    49
    Thanks
    46
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    Again, the key is preprocessing.

    If you have 4 * (3 + 6 / 2 * (9 - 3)) you have order of precedence, in that you look at the inner most parenthesis first, then head outwards. In the same way, code is always by order of precedence, in that inner most code is computed first (most often) and then outer code.

    So if you have monthArray[NUM - 1] first it figures out NUM - 1 (the inner set of code). It doesn't care that its looking for an array index yet, it just knows NUM - 1 has to be calculated. So then it realizes that for NUM = 12, that is 11, so it goes out a layer and sees monthArray[11], and realizes, oh, this is an array, let me try to pull the value of monthArray indexed at 11.
    So to summarize, you're saying in the line of code below, the interpreter looks at the whole function as a block of code, and then first looks inside all parenthetical statements first, and resolves them first, and then start back at the top again and goes down to the array? For this example, the interpreter sees [NUM-1] first and resolves it, and then goes back up to the top and starts again?

    PHP Code:
    Function MonthAsString(NUM)
    {
    var 
    aryMonths = new Array();
    aryMonths[0] = "January";
    aryMonths[1] = "February";
    aryMonths[2] = "March";
    aryMonths[3] = "April";
    aryMonths[4] = "May";
    aryMonths[5] = "June";
    aryMonths[6] = "July";
    aryMonths[7] = "August";
    aryMonths[8] = "September";
    aryMonths[9] = "October";
    aryMonths[10] = "November";
    aryMonths[11] = "December";

    return 
    aryMonths[NUM-1];

    Last edited by ryanjohnsond; 06-26-2012 at 02:33 AM.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Quote Originally Posted by ryanjohnsond View Post
    So to summarize, you're saying in the line of code below, the interpreter looks at the whole function as a block of code, and then first looks inside all parenthetical statements first, and resolves them first, and then start back at the top again and goes down to the array? For this example, the interpreter sees [NUM-1] first and resolves it, and then goes back up to the top and starts again?

    PHP Code:
    Function MonthAsString(NUM)
    {
    var 
    aryMonths = new Array();
    aryMonths[0] = "January";
    aryMonths[1] = "February";
    aryMonths[2] = "March";
    aryMonths[3] = "April";
    aryMonths[4] = "May";
    aryMonths[5] = "June";
    aryMonths[6] = "July";
    aryMonths[7] = "August";
    aryMonths[8] = "September";
    aryMonths[9] = "October";
    aryMonths[10] = "November";
    aryMonths[11] = "December";

    return 
    aryMonths[NUM-1];

    In a word, "NO."

    Step by step.

    JS sees the declaration var aryMonths, so it puts "aryMonths" into an internal table as the name of a known variable.

    Then it sees = new Array() and adds more information to that internal table: It now knows that the variable "aryMonths" is an array, so it allocates a POINTER (don't worry what that means) to an array. Initially, that pointer is null.

    Then it sees aryMonths[0] = "January"; It knows that "aryMonths" is an array, but checking the internal table, and it sees that no memory has yet been allocated for the array, so it allocates space for one element (element zero, in this case). It also sees the string "January" so it allocates internal space to store the internal representation of that string. And then it puts a POINTER to that string's internal representation into element zero of the array (at this point, the only element of the array).

    Then it sees aryMonths[1] = "February"; It knows that "aryMonths" is an array, and checking the internal table, and it sees that moemory has been allocated for only element zero of the array, so it allocates space for two elements (element zero and one, in this case). It copies the old element zero into the new space for element zero and throws away the old one-element array. It also sees the string "February" so it allocates internal space to store the internal representation of that string. And then it puts a POINTER to that string's internal representation into element one (1) of the array.

    It repeats the above for the 12 elements of the array you have defined. It ends up with an array of 12 POINTERS to the 12 internal representations of the strings. In turn, in the table of variables, "aryMonths" has a POINTER to that 12-element array.

    Now it sees return aryMonths[NUM-1];

    In order of precendence, it first calculates NUM-1. Then it sees that you are asking for an element (of a number matching that calculated value) of an array and, finally, realizes that you want the array named "aryMonths".

    So it goes into its internal table of variables, finds "aryMonths" and discovers that, indeed, it is an array (so you don't have a type mismatch error) and follows the pointer to the array space to find out the array has 12 elements (numbered 0 to 11). If the calculated array index (in this case, NUM-1 is NOT in that range, then the array element doesn't exist and you have an error. But when it does, it grabs the POINTER that is in that element number and runs off to see what it points to.

    In this case, it points to a string, so it gets the value of that string [which actually happens to be the POINTER to it, but that's a detail you don't care about].

    FINALLY, it sees the return and realize that it should return the value of that string to the caller of the function.

    NOW...

    All the above is actually simplified! In actuality, a JavaScript engine will typically be smart enough to realize that your aryMonths array can be treated as a constant (that is, nothing modifies it). So it may well do all that processing of the array and storing the internal representation thereof ahead of time, before the function is even called. If you will, it may "hoist" the constant code out of the function so it doesn't have to execute it each time the function is called.

    And there are other possible simplifications and optimizations that the engine may do, hidden from you. (Probably none others in this simple case.)

    In short, the engine is smarter than you are about its job. (Smarter than me, too! That's no slur on you!) Really really bright people create computer languages. And they work very very hard to make the languages ever more efficient.
    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.

  • Users who have thanked Old Pedant for this post:

    ryanjohnsond (06-26-2012)

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    p.s.: And I didn't even describe what JS does when it see function MonthAsString(NUM)
    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.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Not so incidentally, if you had coded
    Code:
    var aryMonths = ["January","February","March","April","May","June","July",
                    "August","September","October","November","December"];
    instead, JS could have parsed that, figured out there were 12 array elements, and avoided the incremental growth of the array caused by your as-is code. It's a tiny performance improvement, but a few dozen such improvements *can* make a difference.
    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.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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