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 9 of 9

Thread: sorting numbers

  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts

    sorting numbers

    I am tring to sort some six numbers..the problem is that it doesnt work when
    I use "document.getElementById" instead of "document.write()
    my program is supposed to write six numbers every second..line by line
    Could you get it work ? Thanks



    PHP Code:
    <html>

    <
    body>
       <
    script>
    setInterval("sortN()",1000);  

    function 
    sortN()
    {
    for(var 
    m=1;m<=6;m++)

    arr[m] = Math.floor(Math.random()*49)+1;
     
    document.getElementById("kut").innerHTML+=arr.sort(sortN)+".."+"<br>"
    } }
    </script>
    <div id="kut"></div>    
    <input type="button" value="baslat"onclick="sortN()">
    </body>
    </html> 

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Is this what you need?
    Code:
    <html> 
    <head>
    <script type="text/javascript"> 
    var timer;
    function compareFunc(a, b){ //sort in ascending order
      return a - b;
    }
    function sortN() 
    { 
      var arr=[];
      for(var m=1;m<=6;m++) 
      {  
        arr[m] = Math.floor(Math.random()*49)+1; 
      } 
      document.getElementById("kut").innerHTML += arr.sort(compareFunc)+".."+"<br />";
      timer = setTimeout("sortN()",1000);
    } 
    </script> 
    </head>
    <body>
    <form>
    <input type="button" value="baslat" onclick="sortN()" /> 
    <input type="button" value="stop" onclick="clearTimeout(timer)" />
    <div id="kut"></div>
    </form>
    </body> 
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks...a lot...you are perfect
    it worked for me....

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    hi glenngv,
    Your code works fine...Now there is a slight problem...some numbers are dublicated...Is there a way to avoid dublications or clearing the lines which cosist dublicated numbers ?

    Many thanks in advance..

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <html> 
    <head>
    <script type="text/javascript"> 
    Array.prototype.inArray = function(element){
      for (var i=0; i<this.length; i++){
        if (this[i]==element){
           return true;
        }
      }
      return false;
    }
    
    
    var timer;
    var size = 6; 
    
    function compareFunc(a, b){ //sort in ascending order
      return a - b;
    }
    function sortN() 
    { 
      var num, arr=[], m=0;
      do
      {  
        num = Math.floor(Math.random()*49)+1; 
        if (!arr.inArray(num)) {
          arr[m] = num;
          m++;  
        }
      } 
      while (m<size)
      document.getElementById("kut").innerHTML += arr.sort(compareFunc)+".."+"<br />";
      timer = setTimeout("sortN()",1000);
    } 
    </script> 
    </head>
    <body>
    <form>
    <input type="button" value="baslat" onclick="sortN()" /> 
    <input type="button" value="stop" onclick="clearTimeout(timer)" />
    <div id="kut"></div>
    </form>
    </body> 
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    wow..you are unbelievable...
    it works......thanks indeed

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hi glenngv,
    I think I need some more describtions about what those lines I coloured green mean...If you could write some commend next to those lines I will be very happy.
    .Thanks in advance


    Quote Originally Posted by glenngv View Post
    Code:
    <html> 
    <head>
    <script type="text/javascript"> 
    Array.prototype.inArray = function(element){
      for (var i=0; i<this.length; i++){
        if (this[i]==element){
           return true;
        }
      }
      return false;
    }
    
    
    var timer;
    var size = 6; 
    
    function compareFunc(a, b){ //sort in ascending order
      return a - b;
    }
    function sortN() 
    { 
      var num, arr=[], m=0;
      do
      {  
        num = Math.floor(Math.random()*49)+1; 
        if (!arr.inArray(num)) {
          arr[m] = num;
          m++;  
        }
      } 
      while (m<size)
      document.getElementById("kut").innerHTML += arr.sort(compareFunc)+".."+"<br />";
      timer = setTimeout("sortN()",1000);
    } 
    </script> 
    </head>
    <body>
    <form>
    <input type="button" value="baslat" onclick="sortN()" /> 
    <input type="button" value="stop" onclick="clearTimeout(timer)" />
    <div id="kut"></div>
    </form>
    </body> 
    </html>

  • #8
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think I can explain these for you:

    The first green line declares a method of all Arrays. It is a function that takes one argument, an Object, and returns a Boolean (2nd/3rd green lines) indicating whether or not that Object is in the Array.

    The fourth green line uses Array literal notation to define an empty Array.

    The last green line checks to see if the random Number is not in the previously defined Array, using the prototype method defined at the top.

    Does that help? Search for 'javascript prototype' and 'javascript literal notation' (no quotes) to learn more about those concepts.
    Last edited by 1212jtraceur; 06-16-2007 at 05:00 PM. Reason: typos

  • #9
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks....it helps..
    I should search for more sample of the same code...

    I like javascript!!!!!


  •  

    Posting Permissions

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