JavaScript - Random Name Generator

Learn on how to create a Random Name Generator using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. This code can be used as your converter to your mathematical problem. So Let's do the coding...

Getting started:

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 - Random Name Generator</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <button class="btn btn-primary btn btn-block" onclick="generate()">Generate Name</button>
  19. <div id="result"></div>
  20. </div>
  21. </div>
  22. <script src="js/script.js"></script>
  23. </body>
  24. </html>

Creating the Script

This code contains the script of the application. This code will generate a random name of a person 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 folder.
  1. function generate(){
  2. var firstname = ["Marquis", "Samir", "Adrien", "Joyce", "Pierce", "Juliette", "Kelton", "Jacob", "Isiah", "Lindsay", "Kian", "Jordyn", "Jaquan", "Anya", "Wayne", "Khalil"];
  3. var lastname= ["Mills", "Mercer", "Reeves", "Hines", "Sanford", "Irwin", "Koch", "Hinton", "Estes", "Jackson", "Lowe", "Guerra", "Pineda", "Franco", "Cowan", "Krause"];
  4. var rand_first = Math.floor(Math.random()*firstname.length);
  5. var rand_last = Math.floor(Math.random()*lastname.length);
  6. document.getElementById('result').innerHTML = "<h1>My name is:</h1><div class='alert alert-success'><h2>"+firstname[rand_first]+" "+lastname[rand_last]+"</h2></div>";
  7.  
  8. }
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!

Comments

Submitted bysidrah khan (not verified)on Fri, 04/15/2022 - 01:19

butter

Add new comment