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.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Array Help - Wish I could be more specific

    Hello, I'm new here and was hoping for a little help with something. I have no background in programming anything, so please don't shoot me down for using bad terms or thinking in a completely wrong way.

    I've been reading parts of tutorials and other peoples' code in order to get a basic grasp of the language, and have run into the problem which follows.

    One array is like this:
    Code:
    typ = new Array();
    typ[0] = ("One");
    typ[1] = ("Two");
    typ[2] = ("Three");
    Another is like this:
    Code:
    var item = new Array();
    function obj_item(name, type)
    //^Questioning the above - does obj_xx need to be the same as the array?  Remember, I'm mostly learning from other peoples' code.
    {
    	this.name = name;
    	this.type = type;
    }
    item[0] = new obj_ar2("Item1",1);
    What I'd like to do is have "type" from the "item" array display what is in the "typ" array.
    Code:
    document.write(item[0].type);
    just returns "1", and I'd like to know how to return "Two". So I'm getting the right value, I just don't know how to use that value to write what is in the other array.

    I've been trying things for hours, and searched all over the place, but not really knowing what I'm doing doesn't help as far as search terms go. Hopefully someone will understand what I'm trying to say/do.

    Thanks a lot for any help, please remember I'm quite clueless and will probably be asking more questions in future.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alright first of all you only have one array.

    item[]

    thats your array.

    now an array is a list right? the things in the list are "elements".

    so item[0] is an element and item[1] is another element.

    now when you make an array its best to say how long the list is so you go
    Code:
    var item=new Array(12);
    now you have an array with 12 elements from 0 to 11. Now we put things inside the array. Your using objects and i really dont think you should be doing that because you probably dont know what an object is. so we will just put some words in the array

    Code:
    item[0]="one";
    item[1]="two";
    item[2]="three";
    if you want two you go document.writeln(item[1]);

    Now are you just wanting to know this or do you have something your trying to make?

    Hope that helps
    UNDER CONSTRUCTION

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply.

    I'm trying to make a fairly advanced calculator based on an MMORPG, so I left out the specifics. I'm not too sure I can do it, but I'm having a lot of fun trying .

    Those aren't my exact arrays, I just simplified them as it was only an example. I'm not absolutely sure what an object is, but I do think I have a basic understanding of it. I'll try to explain what I'm trying to do and why I was using objects:

    Say, for example, my item array is a list of dishes in a cookery book. Each dish is of a certain type - let's keep it simple and say food or drink. In the typ array, I have two elements set to food and drink respectively. Inside the item array, I define "type" as 0 or 1 - for food or drink. Now, my problem is displaying the words in the typ array instead of the numbers in the item array.

    So using the example above, I'll also mention that I have a list of ingredients - let's say the array is named "ingr". I have a tomato listed as the first element in that array, and there are 4 tomatoes in one of the dishes. I believe this is done by using something like [0, 4] in the item array (or should that be object?), but I haven't gotten to that yet. This part is my main reason for using objects, so that's why I mentioned it.
    Last edited by Confused Idiot; 10-25-2006 at 01:27 AM. Reason: Remembered that it would be "[0,4]" rather than "[1, 4]", should this actually work.


  •  

    Posting Permissions

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