Aman Shankar
Aman Shankar

Reputation: 1

Is there a way of creating a constantly changing alphabet between a-z to appear

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

Answers (3)

sourav satyam
sourav satyam

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

Quang Th&#225;i
Quang Th&#225;i

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

user14520680
user14520680

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

Related Questions