SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question need some help with js

    hello there

    I need to do the following: I have 12 sets of leters: a1, a2, b1,b2, c1, c2, d1, d2, e1, e2, f1, f2

    I need a way to show these sets randomly in a row.

    can any1 help me with this plz?! I know javascript can do it but I'm very new to js programing!


    thanx a lot

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tammara...

    Not much of a description...'sets', 'a row', these are unclear. Try again, with more detail.
    ::: certified wild guess :::

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Adios and thanx for reply

    I'm doing a biomedical reseach and have 12 variables: a1, a2, b1, b2 ...
    I need a way to order these variables randomly each time I execute the script. for example I might get the following order :a2, c1, a1, d2, a2 ... and if I execute the script again I might get another order: b2, a2, f1, e2 ...

    plz help me with this!

    thanx

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script>
    var picArray="a1,a2, b1,b2,c1,c2,d1,d2,e1,e2,f1,f2".split("," )
    picCount=picArray.length
    var displayArray=""
    function ShowRandomPics() {
    thisPic=picArray[Math.floor(Math.random()*picCount)];
    if(displayArray.indexOf(thisPic)==-1){if(displayArray!="" ) {displayArray+=","+thisPic; } else 
    displayArray+=thisPic; }
    if(displayArray.split("," ).length!=picArray.length){ ShowRandomPics() } else 
    alert(displayArray)}
    ShowRandomPics() 
    </script>
    The output is an array of your pics, hope its helpfull

    ps. That looks fun to use, a card game perhaps?
    Last edited by Markdidj; Jun 23, 2003 at 14:33.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is relatively (pseudo-)random.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">
    
    body {
          text-align: center;
          background: black;
    }
    
    div#seq {
             width: 100%;
             font: 600 24px monospace;
             color: white;
    }
    
    </style>
    <script type="text/javascript" language="javascript">
    
    var genes = [ 'a1' , 'a2' , 'b1' , 'b2' , 'c1' , 'c2' , 'd1' , 'd2' , 'e1' , 'e2' , 'f1' , 'f2' ];
    
    //Array methods courtesy Vlad Krylov --- www.vladdy.net ----->
    Array.prototype.swap = function(index1,index2) { 
         var temp = this[index1];
         this[index1] = this[index2];
         this[index2] = temp;
         return;
    }
    
    Array.prototype.shuffle = function() { 
         for (var i=0; i<this.length; i++) { 
              ind1 = Math.floor(Math.random() * this.length);
              ind2 = Math.floor(Math.random() * this.length);
              this.swap(ind1,ind2);
       }
         return;
    }
    //----------------------------------------------------------->
    
    var separator = ' | '; //separator
    
    function display(el_id) {
         genes.shuffle();
         var new_seq = document.createTextNode(genes.toString().replace(/,/g, separator));
         var el = document.getElementById(el_id);
         if (el) {
             var old_seq = el.firstChild;
             el.replaceChild(new_seq, old_seq);
       }
    }
    
    </script>
    </head>
    <body>
    <hr>
    <div id="seq">a1 | a2 | b1 | b2 | c1 | c2 | d1 | d2 | e1 | e2 | f1 | f2</div>
    <hr><br>
    <input type="button" style="height:40px;padding-bottom:6px;" value="|  sequence  |" onclick="display('seq')">
    </body>
    </html>
    Last edited by adios; Jun 23, 2003 at 15:50.
    ::: certified wild guess :::


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
  •