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.
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,925
    Thanks
    56
    Thanked 548 Times in 545 Posts
    it appears that earlier IEs don't like the <section> tag much, either

    here is a way of making your script cross-browser (provided you don't have a problem with textNodes, too )

    Code:
    <!DOCTYPE html>
    <html dir="ltr" lang="en">
        <head>
            <meta charset="utf-8"/>
            <title>Table of random alpha characters</title>
            <style>
                #result
                {
                    margin: 10px;
                }
    
                #randomTable
                {
                    border: 1px solid #000000; /* Black. */
                    border-collapse: collapse;
                }
    
                #randomTable td
                {
                    border: 1px solid #000000; /* Black. */
                    text-align: center;
                    height: 50px;
                    width: 50px;
                }
            </style>
        </head>
        <body>
            <article>
                <section id="buttons">
                    <input type="button" id="generateButton" value="Generate"/>
                </section>
                <div id="result"></div>
            </article>
    
            <script>
                /*jslint browser: true, vars: true, white: true, maxerr: 50, indent: 4 */
                (function ()
                {
                    "use strict";
    
                    function removeChildren(parent)
                    {
                        while (parent.hasChildNodes())
                        {
                            parent.removeChild(parent.firstChild);
                        }
                    }
    
    
                    function createTable(id, rows, columns, generator)
                    {
    				var alph = "abcdefghijklmnopqrstuvwxy".split("");
                        var table = document.createElement("table");
                        table.setAttribute("id", id);
    					alph.sort(generator)
                        var body = document.createElement("tbody");
    					var b=0
                        var r = null;
                        var c = null;
                        for (r = 0; r < rows; r += 1)
                        {
                            var row = document.createElement("tr");
    
                            for (c = 0; c < columns; c += 1)
                            {
                                var cell = document.createElement("td");
                                if(cell.hasChildNodes()){
    							cell.removeChild(cell.firstChild)
    									}
    							var txt = document.createTextNode(alph[b++])
    							cell.appendChild(txt);
    
                                row.appendChild(cell);
                            }
    
                            body.appendChild(row);
                        }
    
                        table.appendChild(body);
                        return table;
                    }
    
                    function fetchRandomAlphaCharacter()
                    {
                        return (Math.round(Math.random())-0.5); 
                    }
    
                    function initialize()
                    {
                        var resultSection = document.getElementById("result");
                        var generateButton = document.getElementById("generateButton");
                        generateButton.onclick= function (){
                            removeChildren(resultSection);
    						resultSection.appendChild(createTable("randomTable", 5, 5, fetchRandomAlphaCharacter));
                        }
                    }
    
                    window.onload=initialize;
                }());
            </script>
        </body>
    </html>
    you may note that I reverted to the previous random letter generator as mrhoo's otherwise excellent suggestion creates multiple instances of the same letter, which I don't think OP was talking about...

  2. #17
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by xelawho View Post
    you may note that I reverted to the previous random letter generator as mrhoo's otherwise excellent suggestion creates multiple instances of the same letter, which I don't think OP was talking about...
    Surely if the selection is random then the same letter may (and most probably will) be repeated. I interpret "random letters" to mean that - just the same as "random numbers". That is not at all the same as the alphabet (or 25 out of the 26 letters) listed or displayed in a random order.

    http://graphpad.com/quickcalcs/randomN1.cfm
    Last edited by Philip M; 02-02-2012 at 07:39 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

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