Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2012, 05:45 PM   PM User | #16
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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...
xelawho is offline   Reply With Quote
Old 02-02-2012, 07:25 PM   PM User | #17
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

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.

Last edited by Philip M; 02-02-2012 at 07:39 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
array, generate, letter, random

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.