• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Lottery Number Generator in JavaScript

mrKwinz

Custom Payload Developer
Divine
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
122
Likes
90
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
How to Create a Lottery Number Generator in JavaScript

Introduction

In this tutorial we will create a How to Create a Lottery Number Generator in JavaScript. This tutorial purpose is to teach you how to create a lottery game. This will tackle all the important functionality that allow you to generate a lottery number. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you display a random data. I will give my best to provide you the easiest way of creating this program Lottery Number Generator. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the trigger button and the result list. To create this simply copy and write it into your text editor, then save it 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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Create a Lottery Number Generator</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <h1

    >

    6/58 Lottery Play</

    h1

    >
  19. <button

    class

    =

    "btn btn-primary btn-block"

    onclick

    =

    "rollNumber();"

    >

    Roll A Number</

    button

    >
  20. </

    div

    >
  21. <div

    class

    =

    "col-md-4"

    >
  22. <h3

    >

    Lucky Numbers Result:</

    h3

    >
  23. <div

    id

    =

    "result"

    ></

    div

    >
  24. </

    div

    >
  25. </

    div

    >
  26. <script

    src

    =

    "script.js"

    ></

    script

    >
  27. </

    body

    >
  28. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will generate a random lottery number when you click the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    rollNumber(

    )

    {
  2. var

    set

    =

    [

    ]

    ;
  3. while(

    set

    .length

    <

    6

    )

    {
  4. var

    r =

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    58

    )

    +

    1

    ;
  5. if

    (

    set

    .indexOf

    (

    r)

    ===

    -

    1

    )

    set

    .push

    (

    r)

    ;
  6. }

  7. displayResult(

    set

    )

    ;
  8. }


  9. function

    displayResult(

    set

    )

    {
  10. set

    .join

    (

    ", "

    )

    ;

  11. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    "<center><h1 class='text-primary'>"

    +

    set

    +

    "</h1></center>"

    ;
  12. }

In the code above we just create a method called rollNumber(), this function will generate a random lottery number when triggered. Inside the function we create a blank array object that will hold the numbers. Then we use a loop that will loop through to increment the numbers to 6. We use the function Math.floor() to generate a random number base on the set condition.

Output:

How%20to%20Create%20a%20Lottery%20Number%20Generator%20in%20JavaScript%201.png

The How to Create a Lottery Number Generator in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Lottery Number Generator in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,891

323,899

Top