SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help WIth Random Function for rotating text

    Here's the part I'm having trouble with, its a random function for rotating text.
    I have this under the array to show what last url stands for.
    It stand for the number 0 line of the array.

    var lasturl="0";
    var Orderrand = true;

    Then this part says if the Orderrand is true, then do this. If its true it says "do the random part of this function. " Then apply it to the document.getElementById further below. Here is the function saying do this if Orderrand is true:
    function html(){
    if(Orderrand){index = getRand();
    var rnum;
    index=Math.round(Math.random()*(arr.length-1));
    rnum = index; if (rnum==lasturl) { rnum = getRand();}
    lasturl=rnum; return rnum;}
    else{
    var index = go('rotate_html');
    index = index ? index : 0;
    index = ++index % arr.length;
    }
    (document.getElementById('html_id')).innerHTML =arr[index][0];
    cookie('rotate_html', index);
    }
    If the orderrand function isn't true then do the else. Which says just rotate the array numbers in order instead. The else works just fine as is. I'm only having trouble with this part.
    function html(){
    if(Orderrand){index = getRand();
    var rnum;
    index=Math.round(Math.random()*(arr.length-1));
    rnum = index; if (rnum==lasturl) { rnum = getRand();}
    lasturl=rnum; return rnum;}
    Its supposed to be saying if using the random function don't use to items in the array twice in a row. Can anyone see what I have wrong?

    Next is a copy in another script where this function is working right now, but it has no "else".
    var lasturl="0";
    function complete(){ var index = getRand();
    document.getElementById("arr").innerHTML = arr[index][0]; }
    function getRand () { var rnum;
    var id=Math.round(Math.random()*(arr.length-1));
    rnum = id; if (rnum==lasturl) { rnum = getRand(); }
    lasturl=rnum; return rnum; }
    And here is another script its used in but this one has the else but it can pick the same item twice in a row. This is another working copy that works right now.
    function html(){
    if(Orderrand){
    index = Math.floor(Math.random() * arr.length);
    }else{
    var index = go('rotate_html');
    index = index ? index : 0;
    index = ++index % arr.length;
    }
    (document.getElementById('html_id')).innerHTML =arr[index][0];
    cookie('rotate_html', index);
    }
    So please let me know whats wrong with this.

    var lasturl="0";
    var Orderrand = true;
    function html(){
    if(Orderrand){index = getRand();
    var rnum;
    index=Math.round(Math.random()*(arr.length-1));
    rnum = index; if (rnum==lasturl) { rnum = getRand();}
    lasturl=rnum; return rnum;}
    Sure hope someone can help, thank you very much. Jen
    Last edited by ses5909; Mar 31, 2007 at 22:50. Reason: Removed offer


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
  •