JavaScript Tips
XML Menu Builder
Build powerful, client-side DHTML menus using XML for data and CSS for style. No JavaScript data! No having to place list elements on every single page of your website! No plug-ins!

Creating Random Letters

In our last JavaScript Tip, we showed how to create a random number.

You create a random letter by creating a random number with 26 possibilities — one for each letter of the alphabet. You add this random number as an offset to the starting Unicode character code corresponding to the character "a" or "A" (depending on the case of the random character you want), then convert the result to a character.

The following method below, createRandomLowerCaseLetter(), returns a random lower case letter.

function createRandomLowerCaseLetter()
{
   return String.fromCharCode(97 + Math.round(Math.random() * 25));
}

The following method below, createRandomUpperCaseLetter(), returns a random upper case letter.

function createRandomUpperCaseLetter()
{
   return String.fromCharCode(65 + Math.round(Math.random() * 25));
}