Reputation: 1
something like this but for alphabets:
<p id="result"></p> <!-- this is code for random num gen ,I need something like this for alphabet.-->
<script>
var intrvl = setInterval(numbFunction, 500); //repeat function after 1.5 seconds(1500 ms)
function numbFunction() {
var x = Math.floor(Math.random() * 10 + 1); //return a random no. between 1 to 10
document.getElementById("result").innerHTML = x;
}
</script>
Upvotes: 0
Views: 42
Reputation: 986
setInterval(randomGen, 500);
function randomGen() {
const alphabet = "abcdefghijklmnopqrstuvwxyz"
const random = alphabet[Math.floor(Math.random() * alphabet.length)];
document.getElementById("rand").innerHTML = random;
}
<p id="rand"></p>
Upvotes: 0
Reputation: 697
var intrvl = setInterval(numbFunction, 500);
function numbFunction() {
var characters = 'abcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
var x = Math.floor(Math.random() * charactersLength + 1);
document.getElementById("result").innerHTML = characters[x]
}
<p id="result"></p>
Upvotes: 0
Reputation:
setInterval(function(){
document.getElementById('result').innerHTML = String.fromCharCode(Math.floor(Math.random() * 25) + 97);
}, 500);
Taking advantage of the character code ordering
Upvotes: 2