Random Name Generator Using JavaScript

In this code we will tackle about Random Name Generator using JavaScript. The program will generate a random character from two array objects. The trick of this code is to use Math() in order to randomize the array position. To learn more about this, just follow the steps below.

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">Random Name Generator Using JavaScript</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-6">
  17.                         <form>
  18.                                 <div class="form-group">
  19.                                         <label>Firstname</label>
  20.                                         <input type="text" class="form-control" id="firstname"/>
  21.                                 </div>
  22.                                 <div class="form-group">
  23.                                         <label>Lastname</label>
  24.                                         <input type="text" class="form-control" id="lastname"/>
  25.                                 </div>
  26.                         </form>
  27.                         <center><button class="btn btn-primary" onclick="randomName()">Generate Name</button></center>
  28.                 </div>
  29.         </div>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </html>

Creating the Script

This code contains the script of the application. The code generate a random name when user click the button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function randomName(){
  2.         var firstname = ["John", "Naruto", "Luffy", "Natasha", "Danny"];
  3.         var lastname= ["Wick", "Uzumaki", "Monkey", "Romanof", "Phantom"];
  4.         var rand_first = Math.floor(Math.random()*firstname.length);
  5.         var rand_last = Math.floor(Math.random()*lastname.length);
  6.         var first=document.getElementById('firstname');
  7.         var last=document.getElementById('lastname');
  8.        
  9.         first.value=firstname[rand_first];
  10.         last.value=lastname[rand_last];
  11.        
  12. }
There you have it we successfully created a Random Name Generator 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