Simple Decimal Conversion Using JavaScript

This code will show you how to create a Simple Decimal Conversion using JavaScript. The program will automatically convert the binary number into decimal number. You are free to modify this, and use it in your need. To learn more about this, just follow the steps below.

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.         <nav class="navbar navbar-default">
  6.                 <div class="container-fluid">
  7.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  8.                 </div>
  9.         </nav>
  10.         <div class="col-md-3"></div>
  11.         <div class="col-md-6 well">
  12.                 <h3 class="text-primary">Simple Decimal Conversion Using JavaScript</h3>
  13.                 <hr style="border-top:1px dotted #ccc;"/>
  14.                 <div class="col-md-5">
  15.                         <div class="form-group">
  16.                                 <label>Enter a binary</label>
  17.                                 <input type = "number" id="binary" class="form-control"/>
  18.                         </div>
  19.                         <button class="btn btn-primary" onclick="binaryToDecimal();">Convert</button>
  20.                        
  21.                 </div>
  22.                 <div class="col-md-4">
  23.                         <div id = "result"></div>
  24.                 </div>
  25.         </div>
  26.  
  27. <script src="js/script.js"></script>
  28. </body>
  29. </html>

Creating the Script

This code contains the script of the application. The code will convert the given binary numbers into decimal format. 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.        
  4.         if(binary == ""){
  5.                 alert("Please fill up the required field!");
  6.         }else{
  7.                 if(/^[01]+$/.test(binary)){
  8.                         var decimal = parseInt(binary, 2);
  9.                         document.getElementById("result").innerHTML = "<center><h3 class='text-primary'>"+decimal+"</h3></center>";
  10.                 }else{
  11.                         alert("Please enter binary numbers");
  12.                 }
  13.         }
  14. }
There you have it we successfully created a Simple Decimal Conversion 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!

Add new comment