www.webdeveloper.com
Results 1 to 5 of 5

Thread: easy question - function that yields an entry of an array

  1. #1
    Join Date
    Aug 2014
    Posts
    4

    easy question - function that yields an entry of an array

    Hi,

    excuse me, but I'm quite new and unfamiliar with Javascript. I used the code

    function a(x,y) {
    var array = [[1,2],[3,4]];
    return array[x][y];
    }

    and wanted to use that function in other functions of my .js-file.
    But if I use the command "array(1,2)" within another function I get an error.
    Does anyone know how to fix that Problem?
    Thank you
    Jotwie

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    you should bear in mind that arrays are zero-indexed (meaning that they start at 0, not 1). Also, the variable "array" is local to the "a" function.

    most likely what you are looking for is to call
    Code:
    a(0,1);
    from within your other function

  3. #3
    Join Date
    Aug 2014
    Posts
    4
    Hi,

    you are totally right. I wanted to say that "a(x,y)" doesn't work at all.
    Does anybody know why?
    Thank you
    Jotwie

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by Jotwie View Post
    Hi,

    you are totally right. I wanted to say that "a(x,y)" doesn't work at all.
    Does anybody know why?
    Thank you
    Jotwie
    It's because as already stated array elements start from 0 not 1 and since you have only two elements inside your array array there's no 2 index. So the following code tries to extract element at the index position 2 from the nested array at index position 1. Although the first parameter passed to the a function selects second array element from your array variable, this second array doesn't have any value stored at the 2 index position.

    Code:
    a(1,2) // returns undefined
    Last edited by tech_soul8; 08-06-2014 at 03:59 AM.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,346
    Code:
    // using an object to index like an array...
    function a(x,y) {
        var object = {
                           0:{ 0:1, 
                                1:2
                              },
                           1:{ 0:3, 
                                1:4
                              }
                          };
        return object[x][y];
    }
    Then if you call a(0,1) your output will be 2.

    Objects and Arrays are very similar.

    An alternative would be to pad the array but it is more messy than the alternative of an object.

    Code:
    function a(x,y) {
    var array = [
                      [null,null,null],
                      [null,1,2],
                      [null,3,4]
                    ];
    return array[x+1][y+1]; // adjust referencing
    }
    Then if you call a(1,1) your output will be 4.

    and another way is to...
    Code:
    function a(x,y) {
    var array = [
                      [1,2],
                      [3,4]
                    ];
    return array[x-1][(y-1]; // adjust referencing
    }
    so when you call a(2,1) you get 3 output.

    However... you have no error checking to ensure that negative references are not passed or they exceed the length of the array in question...

    Question about your question, is this a homework question?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles