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')
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))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With