Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing a random letter from a-z

How do I choose a random letter from a-z and put it into a heading in my html by itself and make it replace any other letter that was there before? I don't know if what I've done works.

function randLetter( ) {

var letters =
    ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q"
    ,"r","s","t","u","v","w","x","y","z");

var letter = letters[Math.floor(Math.random()*letters.length)];

return letter

}

$('#letter').html('letter')
like image 531
cparbr Avatar asked Apr 08 '15 03:04

cparbr


2 Answers

For Capital Letters you can use

String.fromCharCode(65+Math.floor(Math.random() * 26))

For Small letters

String.fromCharCode(97+Math.floor(Math.random() * 26))
like image 64
Praveen Kumar Gulati Avatar answered Oct 23 '22 05:10

Praveen Kumar Gulati


Have this piece of code for your work

function randLetter() {
    var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
    var letter = letters[Math.floor(Math.random() * letters.length)];
    return letter
}

$('#letter').html(randLetter())

Fiddle

like image 41
Mritunjay Avatar answered Oct 23 '22 05:10

Mritunjay