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

Advertise Here

Advertise Here

Advertise Here

JavaScript - Convert Binary To Decimal

Jamiew1103

Organic Growth Wizard
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
113
Likes
158
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
In this tutorial we will create a Convert Binary To Decimal using JavaScript. This code will convert the given binary into decimal when the user click the convert button. The code use onclick() function to parse the given value as int in order to convert the value into decimal by adding it as an argument parseInt(binary, 2). Feel free to modify and apply it in your system, this is a user-friendly kind of program

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

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

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  4. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  5. <body

    >
  6. <nav class

    =

    "navbar navbar-default"

    >
  7. <div

    class

    =

    "container-fluid"

    >
  8. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  9. </

    div

    >
  10. </

    nav>
  11. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  12. <div

    class

    =

    "col-md-6 well"

    >
  13. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Convert Binary To Decimal</

    h3

    >
  14. <hr

    style

    =

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

    /

    >
  15. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  16. <div

    class

    =

    "col-md-6"

    >
  17. <div

    class

    =

    "form-group"

    >
  18. <label

    >

    Enter a binary</

    label

    >
  19. <input

    type

    =

    "number"

    id

    =

    "binary"

    class

    =

    "form-control"

    /

    >
  20. </

    div

    >
  21. <center

    ><button

    class

    =

    "btn btn-primary"

    onclick

    =

    "binaryToDecimal();"

    >

    Convert</

    button

    >

    <button

    class

    =

    "btn btn-success"

    onclick

    =

    "reset();"

    >

    Reset</

    button

    ></

    center

    >

  22. <br

    ><br

    >
  23. <div

    id

    =

    "result"

    ></

    div

    >
  24. </

    div

    >
  25. </

    div

    >

  26. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  27. </

    body

    >
  28. </

    html

    >

Creating the Script

This code contains the script of the application. This code will convert the binary into decimal when the button is clicked. 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

    binaryToDecimal(

    )

    {
  2. var

    binary =

    document.getElementById

    (

    "binary"

    )

    .value

    ;

  3. if

    (

    binary ==

    ""

    )

    {
  4. alert(

    "Please fill up the required field!"

    )

    ;
  5. }

    else

    {
  6. if

    (

    /^[01]+$/

    .test

    (

    binary)

    )

    {
  7. var

    decimal =

    parseInt(

    binary,

    2

    )

    ;
  8. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    "<center><label style='font-size:18px;'>The decimal number is</label></center><center><h3 class='text-primary'>"

    +

    decimal+

    "</h3></center>"

    ;
  9. }

    else

    {
  10. alert(

    "Please enter binary numbers"

    )

    ;
  11. }
  12. }
  13. }

  14. function

    reset(

    )

    {
  15. document.getElementById

    (

    "decimal"

    )

    .value

    =

    ""

    ;
  16. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    ""

    ;
  17. }

There you have it we successfully created a Convert Binary To Decimal 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,496

342,218

342,226

Top