SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist jor133d's Avatar
    Join Date
    Jul 2003
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Linking in Arrays

    Hello-

    I got help last week with a randomization problem here on the boards but I now have another question that I hope someone can help me with.

    Code:
    <script language="JavaScript">
    <!-- Hide this script from old browsers --
    
    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
    numbers.sort(function(){return Math.random() - 0.5;});
    var item1 = numbers[0];
    var item2 = numbers[1];
    var item3 = numbers[2];
    </script>
    Right now this code obviously sorts and array and displays the items randomly and store the values in a variable so I can use them later in the page.

    How can I create another array (or use the same array) and link a text value to it?

    For example.

    1, "This is the text linked to number 1"
    2, "This is the text linked to number 2"
    and so on.

    After the numbers are sorted (and randomized) I want the text to be still be linked to the number and stored in a variable. The reason being, I have random video links show up on my page and I want the text connected to the number because the number is an index value I pass into a flash application and they text describes the video.

    Does this make sense?

    Any ideas?

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?

    Code:
    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
    numbers.sort(function(){return Math.random() - 0.5;});
    var item1 = numbers[0];
    var item2 = numbers[1];
    var item3 = numbers[2];
    var linkText = ['one','two', ... , 'eleven'];
    alert('The text linked to ' + item2 + ' is ' + linkText[item2]);


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
  •