JavaScript - Display Random Character Name

In this tutorial we will create a Display Random Character Name using JavaScript. This code will generate a random character name when the user click the button. The code itself use a onclick() to call a specific method that can generate a random character name using Math.floor() and passing an argument Math.random() multiply by an array length to display a random array index value. You can apply this script to your code, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user. .

Getting Started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Display Random Character Name</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3">
  17.                         <center><button class="btn btn-primary" onclick="generateCharacter()">Generate Character</button></center>
  18.                         <br />
  19.                         <center><a href="index.html" class="btn btn-success">Reset</a></center>
  20.                 </div>
  21.                 <div class="col-md-6">
  22.                         <div id="result"></div>
  23.                 </div>
  24.         </div>
  25. <script src="js/script.js"></script>
  26. </body>
  27. </html>

Creating the Script

This code contains the script of the application. This code will generate a random character name when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var count=1;
  2. function generateCharacter(){
  3.         var html;
  4.         var firstname = ["Tony", "Bruce", "Peter", "Steve", "Reed"];
  5.         var lastname= ["Stark", "Banner", "Parker", "Roger", "Richards"];
  6.         var first_random = Math.floor(Math.random()*firstname.length);
  7.         var last_random = Math.floor(Math.random()*lastname.length);
  8.         html="<h5>Character "+count+"</h5>"
  9.         +"<h3>"+firstname[first_random]+" "+lastname[last_random]+"</h3>"
  10.         +"<hr style='border-top:1px solid #000;'/>";
  11.         document.getElementById('result').innerHTML+=html;
  12.        
  13.         count++;
  14.        
  15.        
  16. }
There you have it we successfully created a Display Random Character Name using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment