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

JavaScript - Simple Odd And Even Checker

Othmane755566

Banana Enthusiast
Divine
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
97
Likes
177
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Simple Odd And Even Checker using JavaScript. This code can detect and display the difference between the odd and even based on the given number. This code use onclick() function to trigger a mathematical algorithm to calculate the given value in the text box . You can apply this script to your code to make your transaction faster, 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:

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. <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"

    >

    JavaScript - Simple Odd And Even Checker</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  18. <div

    class

    =

    "col-md-6"

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <input

    type

    =

    "number"

    min=

    "0"

    class

    =

    "form-control"

    id

    =

    "digit"

    /

    >
  21. </

    div

    >
  22. <center

    ><button

    class

    =

    "btn btn-primary"

    onclick

    =

    "checkNumber();"

    >

    Check</

    button

    >

    <button

    class

    =

    "btn btn-success"

    onclick

    =

    "reset();"

    >

    Reset</

    button

    ></

    center

    >
  23. <br

    /

    ><br

    /

    >
  24. <div

    id

    =

    "result"

    ></

    div

    >
  25. </

    div

    >
  26. </

    div

    >
  27. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  28. </

    body

    >
  29. </

    html

    >

Creating the Script

This code contains the script of the application. This code will calculate the odd and even numbers 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. function

    reset(

    )

    {
  2. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    ""

    ;
  3. document.getElementById

    (

    "digit"

    )

    .value

    =

    ""

    ;
  4. }

  5. function

    checkNumber(

    )

    {
  6. var

    input =

    document.getElementById

    (

    "digit"

    )

    .value

    ;
  7. var

    number =

    parseInt(

    input)

    ;
  8. if

    (

    isNaN(

    number)

    )

    {
  9. alert(

    "Please enter a number."

    )

    ;
  10. }

    else

    if

    (

    number %

    2

    ==

    0

    )

    {
  11. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    "<center><label style='font-size:25px;' class='text-success'>"

    +

    number+

    "</label> <label style='font-size:18px;'>is even number</label></center>"

    ;
  12. }
  13. else

    {
  14. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    "<center><label style='font-size:25px;' class='text-danger'>"

    +

    number+

    "</label> <label style='font-size:18px;'>is odd number</label></center>"

    ;
  15. }
  16. }

There you have it we successfully created a Simple Odd And Even Checker 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!


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

452,292

324,242

324,250

Top