Results 1 to 2 of 2

Thread: Array Issue

  1. #1
    Join Date
    Mar 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Issue

    Ok this might be a bit complicated (or not) but...

    I have data coming in from an external source that automatically assigns a number value to an action. I have to check to see if a variable equals that number then I have to change that variable to equal the text value.

    This is the only way I can think to do it (but there has to be a better way...):

    Code:
    if (photoCaptionID0 == "24001"){
    photoCaptionID0 = "Bat Ray Swimming"
    }
    
    //72 entries later...
    
    if (photoCaptionID0 == "24073"){
    photoCaptionID0 = "Blue Shark Swimming"
    }
    
    if (photoCaptionID1 == "24001"){
    photoCaptionID1 = "Bat Ray Swimming"
    }
    
    //72 entries later...
    
    if (photoCaptionID1 == "24073"){
    photoCaptionID1 = "Blue Shark Swimming"
    }
    
    //Do this for photoCaptionID2-5
    
    var photoCaptionIDZero = document.getElementById('photoCaptionID0');
    if (photoCaptionIDZero)
    photoCaptionIDOne.innerHTML = photoCaptionID0;
    
    var photoCaptionIDOne = document.getElementById('photoCaptionID1');
    if (photoCaptionIDOne)
    photoCaptionIDOne.innerHTML = photoCaptionID1;
    
    //Do this for 2-5
    That would be about 400 lines of code. Is there a way to use arrays to achieve the same thing?

    --thesprucegoose

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,372
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    You could make an array:

    Code:
    var swimmers = [
    "Bat Ray Swimming",
    "Something Else Swimming",
     . . .
    "Blue Shark Swimming" //no comma after the last entry
    ];
    You should end up with 73 entries in the array. They will be known as swimmers[0] through swimmers[72]. Then you could do:

    Code:
    photoCaptionID0 = swimmers[photoCaptionID0 - 24001];
    photoCaptionID1 = swimmers[photoCaptionID1 - 24001];
    photoCaptionID2 = swimmers[photoCaptionID2 - 24001];
    photoCaptionID3 = swimmers[photoCaptionID3 - 24001];
    photoCaptionID4 = swimmers[photoCaptionID4 - 24001];
    photoCaptionID5 = swimmers[photoCaptionID5 - 24001];
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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