Home / Programs / Create random character using Javascript
🚀 Programming Example

Create random character using Javascript

👁 333 Views
💻 Practical Program
📘 Step Learning
Create random character using Javascript. This code will help you to generate random character with a specific length.

💻 Program Code

<html>
<body>
<div>
Enter the length of character:
<input type='text' id="num">
<button onclick="stringGen()">submit</button>
<p id="result"></p>
</div>
<script type="text/javascript" src="index.js"></SCRIPT>
</body>
</html>


function stringGen()
{
  var length = document.getElementById("num").value;
  // alert(length);
   var result           = '';
   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   var charactersLength = characters.length;
   for ( var i = 0; i < length; i++ ) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   document.getElementById("result").innerHTML = result; 
   return result;
}





                        

🖥 Program Output

above code will produce random character of a specific length.
                            

📘 Explanation

No
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.