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

JavaScript - Simple Leap Year Checker

Herossandro

Critical Infrastructure Pentester
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
159
Likes
28
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Simple Leap Year Checker using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any 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. This code will render application and display the form. 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 Leap Year 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"

    id

    =

    "year"

    class

    =

    "form-control"

    placeholder=

    "Enter a year here..."

    /

    >
  21. </

    div

    >
  22. <center

    ><button

    class

    =

    "btn btn-primary"

    onclick

    =

    "yearCheck(this);"

    id

    =

    "check"

    >

    Check</

    button

    ></

    center

    >
  23. <center

    ><button

    class

    =

    "btn btn-success"

    onclick

    =

    "reset(this);"

    style

    =

    "display:none;"

    id

    =

    "reset"

    >

    Reset</

    button

    ></

    center

    >
  24. <br

    /

    >
  25. <div

    id

    =

    "result"

    ></

    div

    >
  26. </

    div

    >
  27. </

    div

    >
  28. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  29. </

    body

    >
  30. </

    html

    >

Creating the Script

This code contains the script of the application. This code will check whether the year has a leap year. To do this just copy write the code as shown below inside the text editor and save it inside the js directory as script.js.
  1. function

    yearCheck(

    btn)

    {
  2. var

    year =

    document.getElementById

    (

    'year'

    )

    .value

    ;

  3. if

    (

    year ==

    ""

    )

    {
  4. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    "<center><label class='text-danger'>Please enter a valid year</label></center>"

    ;
  5. }

    else

    {

  6. if

    (

    !

    (

    year %

    4

    )

    &&

    (

    year %

    100

    )

    ||

    !

    (

    year %

    400

    )

    )

    {
  7. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    "<div class='alert alert-success' style='text-align:center;'>Year <label class='text-primary'>"

    +

    year+

    "</label> is a LEAP YEAR</div>"

    ;
  8. }

    else

    {
  9. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    "<div class='alert alert-warning' style='text-align:center;'>Year <label class='text-danger'>"

    +

    year+

    "</label> is a NOT LEAP YEAR</div>"

    ;
  10. }

  11. btn.style

    .display

    =

    'none'

    ;
  12. document.getElementById

    (

    'reset'

    )

    .style

    .display

    =

    ''

    ;
  13. document.getElementById

    (

    'year'

    )

    .setAttribute

    (

    'readonly'

    ,

    'readonly'

    )

    ;
  14. }
  15. }

  16. function

    reset(

    btn)

    {
  17. document.getElementById

    (

    'year'

    )

    .value

    =

    ""

    ;
  18. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    ""

    ;

  19. btn.style

    .display

    =

    'none'

    ;
  20. document.getElementById

    (

    'check'

    )

    .style

    .display

    =

    ''

    ;
  21. document.getElementById

    (

    'year'

    )

    .removeAttribute

    (

    'readonly'

    )

    ;
  22. }

There you have it we successfully created a Simple Leap Year Checker using JavaScript. I hope that this very 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 hidden text.
 

452,292

323,348

323,357

Top