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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts

    what am I missing?

    I tested the following script but it did not work
    Could you please fix the problem ? so I can learn more about firstchild and node...
    Thanks


    PHP Code:

    <html>

    <
    head>
    <
    script>
    window.onload = function() 
    {
        
    sortN();
    }

    function 
    sortN() {
        for(var 
    1<= 6m++) {
            
    arr[m] = Math.floor(Math.random() * 49) + 1;
        }
        
    arr.sort();
        var 
    sorted;
        for(var 
    1<= 6m++) {
            
    sorted += arr[m] + "\n";
        }
        var 
    kut document.getElementById('kut');
        if (
    kutkut.firstChild.nodeValue += sorted;

    }


    </script>
    </head>
    <body>
            <pre id="kut">&nbsp;</pre>
    </body>
    </html> 

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <html>

    <head>
    <script type = "text/JavaScript">
    function sortN() {
    var arr = new Array();
    for (var m = 1; m <= 6; m++) { // array values index 1-6
    var x = Math.floor(Math.random() * 49) + 1;
    if (x<10) {x = "0" + x} // add leading zero to single digits for sort
    arr[m] = x;
    }


    arr.sort(); // sort() is performed as ASCII not numeric
    var sorted = ""; // initialise value

    for (var m = 0; m <= 5; m++) { // sorted array index 0 -5
    var y = arr[m].toString(); // must be a string now
    var z = y.substr(0,1); // identify first character
    if (z == "0") {y = y.substr(1,1)} // delete leading zeroes again
    sorted += y + "\n";
    }


    var kut = document.getElementById('kut');
    if (kut) kut.firstChild.nodeValue += sorted;
    }
    </script>
    </head>
    <body onload="sortN()" >
    <pre id="kut">&nbsp;</pre>
    </body>
    </html>

    P.S. kut is a naughty word in Dutch!
    Last edited by Philip M; 06-18-2007 at 06:24 PM. Reason: Highlighted changes

  • #3
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    To clarify Philip's code (if you didn't see it, gencoglu), you didn't declare arr before assigning properties to it.

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks for your reply..Now what we get is only one single line of six numbers..
    I will add a "setInterval ()" function to write one line at a time..and I need line under line...
    I tried "<br>" but it did not work..Can we use "<div>" instead of "<pre>" ?
    whats more..sometime there are dublicated numbers..so we need to avoid them..
    thanks
    Last edited by gencoglu; 06-19-2007 at 01:02 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,920
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here's the modification to eliminate duplicate numbers:-

    function sortN() {
    var arr = new Array();
    for (var m = 1; m <= 6; m++) { // array values index 1-6
    var x = Math.floor(Math.random() * 49) + 1;
    arr[m] = x;
    if (m > 0) {
    for (var j=0; j<arr.length-1; j++) {
    if (x == arr[j]) {m = m-1; break} //check for repeated elements and get another number if duplicated
    }
    }

    if (x < 10) {x = "0" + x} // add leading zero to single digits for sort
    arr[m] = x;
    }

    sorted is a single variable containing six concatenated numbers so you cannot write it out line by line.

    You need:-

    for (j=0; j<=5; j++) { // 6 data values
    document.write (arr[j] + "<br>");
    }
    Last edited by Philip M; 06-19-2007 at 04:11 PM. Reason: Improvement


  •  

    Posting Permissions

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